-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxCounters.java
More file actions
33 lines (28 loc) · 967 Bytes
/
MaxCounters.java
File metadata and controls
33 lines (28 loc) · 967 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
// Max Counters: handle N counters based on the elements of an array
// https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/
class MaxCounters {
public int[] solution(int N, int[] A) {
int max = 0;
int lastIncrease = 0;
int[] counters = new int[N];
for(int element : A){
if(element > N){
lastIncrease = max;
} else {
if(counters[element-1] < lastIncrease){
counters[element-1] = lastIncrease;
}
++counters[element-1];
if (max < counters[element-1]) {
max = counters[element-1];
}
}
}
for (int i=0; i<N; i++) {
if(counters[i] < lastIncrease) {
counters[i] = lastIncrease;
}
}
return counters;
}
}