Problem
You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on duty on the ith day. The days are numbered starting from 0. You are also given an integer time.
The ith day is a good day to rob the bank if:
- There are at least 
timedays before and after theithday, - The number of guards at the bank for the 
timedays beforeiare non-increasing, and - The number of guards at the bank for the 
timedays afteriare non-decreasing. 
More formally, this means day i is a good day to rob the bank if and only if security[i - time] >= security[i - time + 1] >= ... >= security[i] <= ... <= security[i + time - 1] <= security[i + time].
Return a list of all days (0-indexed) that are good days to rob the bank. The order that the days are returned in does not matter.
Example 1:
1  | Input: security = [5,3,3,3,5,6,2], time = 2  | 
Example 2:
1  | Input: security = [1,1,1,1,1], time = 0  | 
Example 3:
1  | Input: security = [1,2,3,4,5,6], time = 2  | 
Example 4:
1  | Input: security = [1], time = 5  | 
Constraints:
1 <= security.length <= 1050 <= security[i], time <= 105
Analysis
  又是抢劫银行的题目,但这个不是打家劫舍的系列题了,所以并不是无脑的dp。先来看看题目要求,题目给出一个数组security,定义good day为某天的前time天都是非递增的,后time天都是非递减的,问数组中所有的good day。首先看最直接的做法就是遍历一遍数组,对每个位置检查一下前time天和后time天是否满足题目的要求。但这里的问题是有很多重复的计算,比如处理第i天时,已经把前面计算了一遍,当处理第i + 1天时,又需要把前面的再处理一边,这就是需要优化的点。
回顾前面1525. Number of Good Ways to Split a String的优化思路,两道题其实本质是一样的,都要进行预先计算从而减少重复计算。这里提前计算也是有两部分,一个是从左到右非递增的长度,另一个是从右到左非递归的长度,注意单独一个数字也是一个序列。
Solution
无。
Code
1  | class Solution {  | 
Summary
这道题目相当于又给我们复习了通过提前计算减少重复运算的方法,这在处理数组类问题中非常常见。这道题目的分享到这里,感谢你的支持!