-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxCounters.java
More file actions
29 lines (28 loc) · 825 Bytes
/
MaxCounters.java
File metadata and controls
29 lines (28 loc) · 825 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
//Codility MaxCounters
class Solution {
public int[] solution(int N, int[] A) {
// write your code in Java SE 8
int last_max_set = 0;
int max = 0;
int[] array = new int[N];
for (int i = 0;i<A.length;i++){
if (A[i]<=N){
if (array[A[i]-1]<last_max_set){
array[A[i]-1] = last_max_set;
}
array[A[i]-1]++;
if (array[A[i]-1]>max){
max = array[A[i]-1];
}
}else if (A[i]==N+1){
last_max_set = max;
}
}
for (int i = 0;i<N;i++){
if (array[i]<last_max_set){
array[i] = last_max_set;
}
}
return array;
}
}