-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindAllGoodIndices.java
52 lines (44 loc) · 1.64 KB
/
FindAllGoodIndices.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*https://leetcode.com/problems/find-all-good-indices/*/
class Solution {
public List<Integer> goodIndices(int[] nums, int k) {
int n = nums.length;
TreeSet<Integer> nonIn = new TreeSet<Integer>(), nonDec = new TreeSet<Integer>();
for (int i = 0; i < n-1; ++i)
if (nums[i+1] > nums[i])
nonIn.add(i);
for (int i = 0; i < n-1; ++i)
if (nums[i+1] < nums[i])
nonDec.add(i);
List<Integer> result = new ArrayList<Integer>();
for (int i = k; i < n-k; ++i)
{
Integer left = nonIn.lower(i);
if (left != null && left == i-1) left = nonIn.lower(left);
Integer right = nonDec.higher(i);
if ((left != null && left >= i-k) || (right != null && right < i+k)) continue;
else result.add(i);
}
return result;
}
}
class Solution {
public List<Integer> goodIndices(int[] nums, int k) {
int[] incScore = new int[nums.length];
int[] decScore = new int[nums.length];
int score = 0;
for (int i = 1; i < nums.length; ++i){
if (nums[i] <= nums[i - 1])
decScore[i] = decScore[i - 1] + 1;
}
for (int i = nums.length - 2; i >= 0; --i){
if (nums[i] <= nums[i + 1])
incScore[i] = incScore[i + 1] + 1;
}
List<Integer> result = new ArrayList<>();
for (int i = 1; i < nums.length - 1; ++i){
if (incScore[i + 1] >= k - 1 && decScore[i - 1] >= k - 1)
result.add(i);
}
return result;
}
}