解题思路
前缀和思想
两次遍历 用pre记录第i天前非连续递增天数 after记录第i天后非连续递减的天数
如果pre和after都大于time,那么说明第i天符合要求
代码
class Solution {
public List<Integer> goodDaysToRobBank(int[] security, int time) {
List<Integer> ans = new ArrayList<>();
//记录i天前非连续递增的天数
int[] pre = new int[security.length];
//记录i天后非连续递减的天数
int[] after = new int[security.length];
for (int i=1;i<security.length;i++){
if (security[i] <= security[i-1]){
pre[i] = pre[i-1] + 1;
}else {
pre[i] = 0;
}
}
for (int i=security.length-2;i>=0;i--){
if (security[i] <= security[i+1] ){
after[i] = after[i+1] + 1;
}else {
after[i] = 0;
}
}
for (int i=0;i<after.length;i++){
if (pre[i] >= time && after[i] >= time){
ans.add(i);
}
}
return ans;
}
}