As per the instructions are given in MaxCounters-Codility,
You are given N
N
counters, initially set to 0, and you have two possible operations on them:
increase(X)
− counterX
is increased by 1,
max counter
− all counters are set to the maximum value of any counter.
increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. AA non-empty array A
A
of MM
integers is given. This array represents consecutive operations:
if A[K] = X
A[K] = X
, such that 1 ≤ XX
≤ NN
, then operation KK
is increase(X)increase(X)
, if A[K] = N + 1A[K] = N + 1
then operation KK
is max countermax counter
.
I have written this code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N).Its Its currently on O(N*M).
As per the instructions are given in MaxCounters-Codility,
You are given N counters, initially set to 0, and you have two possible operations on them:
increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive operations:
if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), if A[K] = N + 1 then operation K is max counter.
I have written this code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N).Its currently on O(N*M).
As per the instructions are given in MaxCounters-Codility,
You are given
N
counters, initially set to 0, and you have two possible operations on them:
increase(X)
− counterX
is increased by 1,
max counter
− all counters are set to the maximum value of any counter.
A non-empty array
A
ofM
integers is given. This array represents consecutive operations:
if
A[K] = X
, such that 1 ≤X
≤N
, then operationK
isincrease(X)
, ifA[K] = N + 1
then operationK
ismax counter
.
I have written this code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N). Its currently on O(N*M).
As per the instructions are given in MaxCounters-Codility,
You are given N counters, initially set to 0, and you have two possible operations on them:
increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive operations:
if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), if A[K] = N + 1 then operation K is max counter.
I have written thethis code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N).Its currently on O(N*M).
As per the instructions are given in MaxCounters-Codility, I have written the code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N).Its currently on O(N*M).
As per the instructions are given in MaxCounters-Codility,
You are given N counters, initially set to 0, and you have two possible operations on them:
increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive operations:
if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), if A[K] = N + 1 then operation K is max counter.
I have written this code
public int[] maxCount(int[]A,int N) {
int[] I = new int[N];
for (int i = 0; i < A.length; i++) {
try {
I[A[i] - 1]++;
} catch (Exception e) {
Arrays.sort(I);
Arrays.fill(I, I[I.length - 1]);
}
}
return I;
}
It gives correct answers for all test cases. Any Idea to do this with time complexity O(N).Its currently on O(N*M).