Find Good Days to Rob the Bank LeetCode solution in Java

1
class Solution {
2
public List<Integer> goodDaysToRobBank(int[] security, int time) {
3
int[] nonincrease = new int[security.length];
4
for (int i = 1; i < security.length - time; i++) {
5
if (security[i] > security[i - 1]) {
6
nonincrease[i] = 0;
7
} else {
8
nonincrease[i] = nonincrease[i - 1] + 1;
9
}
10
}
11

12
int[] nondecrease = new int[security.length];
13
for (int i = security.length - 2; i >= time; i--) {
14
if (security[i] > security[i + 1]) {
15
nondecrease[i] = 0;
16
} else {
17
nondecrease[i] = nondecrease[i + 1] + 1;
18
}
19
}
20

21
ArrayList<Integer> result = new ArrayList<>();
22
for (int i = time; i < security.length - time; i++) {
23
if (nonincrease[i] >= time && nondecrease[i] >= time) {
24
result.add(i);
25
}
26
}
27

28
return result;
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0