-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxCounters
37 lines (28 loc) · 1.08 KB
/
MaxCounters
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
//MaxCounters
public int[] maxCountersSolution(int N, int[] A) {
final int condition = N + 1;
int currentMax = 0;
int lastUpdate = 0;
int countersArray[] = new int[N];
for (int iii = 0; iii < A.length; iii++) {
int currentValue = A[iii];
if (currentValue == condition) {
lastUpdate = currentMax;
} else {
int position = currentValue - 1;
if (countersArray[position] < lastUpdate)
countersArray[position] = lastUpdate + 1;
else
countersArray[position]++;
if (countersArray[position] > currentMax) {
currentMax = countersArray[position];
}
}
}
for (int iii = 0; iii < N; iii++)
if (countersArray[iii] < lastUpdate)
countersArray[iii] = lastUpdate;
return countersArray;
}
int[] i = {3,4,4,6,1,4,4};
System.out.println("maxCountersSolution : "+Arrays.toString(obj.maxCountersSolution(5, i)));