Input: nums =[2,1,1,1,3,4,1], k =2Output: [2,3]Explanation: There are two good indices in the array:- Index 2. The subarray [2,1]isin non-increasing order, and the subarray [1,3]isin non-decreasing order.- Index 3. The subarray [1,1]isin non-increasing order, and the subarray [3,4]isin non-decreasing order.Note that the index 4is not good because [4,1]is not non-decreasing.
We want to efficiently check for each index if the k elements before it are non-increasing and the k elements after it are non-decreasing. We can precompute for each index how long the non-increasing and non-decreasing sequences are ending or starting at that index using prefix arrays.
classSolution {
public: vector<int> goodIndices(vector<int>& nums, int k) {
int n = nums.size();
vector<int> non_inc(n, 1), non_dec(n, 1), ans;
for (int i =1; i < n; ++i)
if (nums[i] <= nums[i-1]) non_inc[i] = non_inc[i-1] +1;
for (int i = n-2; i >=0; --i)
if (nums[i] <= nums[i+1]) non_dec[i] = non_dec[i+1] +1;
for (int i = k; i < n-k; ++i)
if (non_inc[i-1] >= k && non_dec[i+1] >= k) ans.push_back(i);
return ans;
}
};
classSolution {
public List<Integer>goodIndices(int[] nums, int k) {
int n = nums.length;
int[] nonInc =newint[n], nonDec =newint[n];
Arrays.fill(nonInc, 1); Arrays.fill(nonDec, 1);
for (int i = 1; i < n; i++)
if (nums[i]<= nums[i-1]) nonInc[i]= nonInc[i-1]+ 1;
for (int i = n-2; i >= 0; i--)
if (nums[i]<= nums[i+1]) nonDec[i]= nonDec[i+1]+ 1;
List<Integer> ans =new ArrayList<>();
for (int i = k; i < n-k; i++)
if (nonInc[i-1]>= k && nonDec[i+1]>= k) ans.add(i);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fungoodIndices(nums: IntArray, k: Int): List<Int> {
val n = nums.size
val nonInc = IntArray(n) { 1 }
val nonDec = IntArray(n) { 1 }
for (i in1 until n)
if (nums[i] <= nums[i-1]) nonInc[i] = nonInc[i-1] + 1for (i in n-2 downTo 0)
if (nums[i] <= nums[i+1]) nonDec[i] = nonDec[i+1] + 1val ans = mutableListOf<Int>()
for (i in k until n-k)
if (nonInc[i-1] >= k && nonDec[i+1] >= k) ans.add(i)
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defgoodIndices(self, nums: list[int], k: int) -> list[int]:
n = len(nums)
non_inc = [1] * n
non_dec = [1] * n
for i in range(1, n):
if nums[i] <= nums[i-1]:
non_inc[i] = non_inc[i-1] +1for i in range(n-2, -1, -1):
if nums[i] <= nums[i+1]:
non_dec[i] = non_dec[i+1] +1 ans = []
for i in range(k, n-k):
if non_inc[i-1] >= k and non_dec[i+1] >= k:
ans.append(i)
return ans
impl Solution {
pubfngood_indices(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k asusize;
letmut non_inc =vec![1; n];
letmut non_dec =vec![1; n];
for i in1..n {
if nums[i] <= nums[i-1] {
non_inc[i] = non_inc[i-1] +1;
}
}
for i in (0..n-1).rev() {
if nums[i] <= nums[i+1] {
non_dec[i] = non_dec[i+1] +1;
}
}
letmut ans =vec![];
for i in k..n-k {
if non_inc[i-1] >= k && non_dec[i+1] >= k {
ans.push(i asi32);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
goodIndices(nums: number[], k: number):number[] {
constn=nums.length;
constnonInc= Array(n).fill(1);
constnonDec= Array(n).fill(1);
for (leti=1; i<n; i++)
if (nums[i] <=nums[i-1]) nonInc[i] =nonInc[i-1] +1;
for (leti=n-2; i>=0; i--)
if (nums[i] <=nums[i+1]) nonDec[i] =nonDec[i+1] +1;
constans: number[] = [];
for (leti=k; i<n-k; i++)
if (nonInc[i-1] >=k&&nonDec[i+1] >=k) ans.push(i);
returnans;
}
}