Skip to main content
Code Review

Return to Question

edited tags; edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Tweeted twitter.com/StackCodeReview/status/1040661646253977600
formatting
Source Link
Konrad Rudolph
  • 6.7k
  • 23
  • 35

As per the instructions are given in MaxCounters-Codility,

You are given NN 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.

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 AA of MM integers is given. This array represents consecutive operations:

if A[K] = XA[K] = X, such that 1 ≤ XXNN, 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 ≤ XN, 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) − 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 ≤ XN, 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).

Added code chall desctiption
Source Link
Ludisposed
  • 11.8k
  • 2
  • 41
  • 91

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).

edited title
Link
RAN_0915
  • 153
  • 1
  • 8
Loading
Source Link
RAN_0915
  • 153
  • 1
  • 8
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /