---
# System prepended metadata

title: 2420. Find All Good Indices
tags: [Leetcode, Dynamic Programming, Medium]

---

# 2420. Find All Good Indices
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/find-all-good-indices/
## 思路
nonIncrease记录这个数字前面(包含这个数字)的nonIncreasing sequence的长度
nonDecrease记录这个数字后面(包含这个数字)的nonDecreasing sequence的长度
## Code
```java=
class Solution {
    public List<Integer> goodIndices(int[] nums, int k) {
        List<Integer> ans = new ArrayList<>();
        int n = nums.length;
        int[] nonIncrease = new int[n], nonDecrease = new int[n];
        Arrays.fill(nonIncrease, 1);
        Arrays.fill(nonDecrease, 1);
        for(int i=1; i<n; i++){
            if(nums[i]<=nums[i-1]) nonIncrease[i] = nonIncrease[i-1]+1;
        }
        for(int i=n-2; i>=0; i--){
            if(nums[i]<=nums[i+1]) nonDecrease[i] = nonDecrease[i+1]+1;
        }
        for(int i=k; i<n-k; i++){
            if(nonIncrease[i-1]>=k && nonDecrease[i+1]>=k) ans.add(i);
        }
        return ans;
    }
}
```
