-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_MaxCounters.java
More file actions
35 lines (32 loc) · 864 Bytes
/
2_MaxCounters.java
File metadata and controls
35 lines (32 loc) · 864 Bytes
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
class Solution {
public int[] solution(int N, int[] A) {
int[] B = new int[N];
int max = 0, lastMax = 0;
for(int i = 0; i < A.length; i++) {
int a = A[i] - 1;
// max_counter
if(A[i] > N) {
lastMax = max;
}
else {
if(B[a] < lastMax) {
B[a] = lastMax + 1;
}
// increase(X)
else {
B[a]++;
}
// if a new max occured
if(B[a] > max) {
max = B[a];
}
}
}
for(int j = 0; j < B.length; j++) {
if(B[j] < lastMax) {
B[j] = lastMax;
}
}
return B;
}
}