# 2100. 适合打劫银行的日子

### 题目地址(5935. 适合打劫银行的日子)

<https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/>

### 题目描述

```
你和一群强盗准备打劫银行。给你一个下标从 0 开始的整数数组 security ，其中 security[i] 是第 i 天执勤警卫的数量。日子从 0 开始编号。同时给你一个整数 time 。

如果第 i 天满足以下所有条件，我们称它为一个适合打劫银行的日子：

第 i 天前和后都分别至少有 time 天。
第 i 天前连续 time 天警卫数目都是非递增的。
第 i 天后连续 time 天警卫数目都是非递减的。

更正式的，第 i 天是一个合适打劫银行的日子当且仅当：security[i - time] >= security[i - time + 1] >= ... >= security[i] <= ... <= security[i + time - 1] <= security[i + time].

请你返回一个数组，包含 所有 适合打劫银行的日子（下标从 0 开始）。返回的日子可以 任意 顺序排列。

 

示例 1：

输入：security = [5,3,3,3,5,6,2], time = 2
输出：[2,3]
解释：
第 2 天，我们有 security[0] >= security[1] >= security[2] <= security[3] <= security[4] 。
第 3 天，我们有 security[1] >= security[2] >= security[3] <= security[4] <= security[5] 。
没有其他日子符合这个条件，所以日子 2 和 3 是适合打劫银行的日子。


示例 2：

输入：security = [1,1,1,1,1], time = 0
输出：[0,1,2,3,4]
解释：
因为 time 等于 0 ，所以每一天都是适合打劫银行的日子，所以返回每一天。


示例 3：

输入：security = [1,2,3,4,5,6], time = 2
输出：[]
解释：
没有任何一天的前 2 天警卫数目是非递增的。
所以没有适合打劫银行的日子，返回空数组。


示例 4：

输入：security = [1], time = 5
输出：[]
解释：
没有日子前面和后面有 5 天时间。
所以没有适合打劫银行的日子，返回空数组。

 

提示：

1 <= security.length <= 105
0 <= security[i], time <= 105
```

### 前置知识

* 动态规划

### 公司

* 暂无

### 思路

对于每一个位置 i ，我们如何判断其是否适合打劫呢？显然我们需要知道：

1. i 前面有多少小于等于当前位置的连续位置个数。
2. i 后面有多少大于等于当前位置的连续位置个数。

因此我们可以先进行一次预处理，将上面的两个信息求出来。不妨使用两个数组 l 和 r 分别存储。比如 l\[i] 表示 i 左侧有多少个连续位置是小于等于 security\[i] 的。

接下来我们只需要遍历一次 security 就可以判断出每一个位置是否适合打劫。如果适合打劫就加入到结果数组 ans 中。

### 关键点

* 预处理出数组 l 和 r

### 代码

* 语言支持：Python3

Python3 Code:

```python

class Solution:
    def goodDaysToRobBank(self, security: List[int], time: int) -> List[int]:
        n = len(security)
        l, r = [0]*n, [0]*n
        ans = []

        for i in range(1, n):
            if security[i] <= security[i-1]:
                l[i] += l[i-1] + 1
        for i in range(n-2,-1,-1):
            if security[i] <= security[i+1]:
                r[i] += r[i+1] + 1

        for i in range(n):
            if l[i] >= time and r[i] >= time:
                ans.append(i)
        return ans

```

**复杂度分析**

令 n 为数组长度。

* 时间复杂度：$O(n)$
* 空间复杂度：$O(n)$

> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。

力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/)，这样就会第一时间收到我的动态啦\~

以上就是本文的全部内容了。大家对此有何看法，欢迎给我留言，我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库：<https://github.com/azl397985856/leetcode> 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。

关注公众号力扣加加，努力用清晰直白的语言还原解题思路，并且有大量图解，手把手教你识别套路，高效刷题。

![](https://p.ipic.vip/6k17en.jpg)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/medium/5935.find-good-days-to-rob-the-bank.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
