c# - MaxCounters codility understanding -
i have wanted try challenges on codility , started beginning. assignments relatively easy 1 called maxcounters. not believe 1 hard although first 1 marked not painless.
i have read task , started coding in c# language:
public static int[] maxpart(int n, int[] a){ int[] counters = new int[n]; for(int = 0; < a.length; i++){ for(int j = 0; j < counters.length; j++){ if(a[i] == counters[j] && (counters[j] >= 1 && counters[j] <= n )){ counters [j] = counters [j] + 1; } if(a[i] == n + 1 ){ int tmpmax = counters.max (); for(int h = 0; h < counters.length; h++){ counters [h] = tmpmax; } } } } return counters; }
having 3 loops of course makes slow, lets leave later. concern how understood , other people see on question here.
from assignment's description.
it has 2 actions:
- increase(x) − counter x increased 1,
- max counter − counters set maximum value of counter.
which occur under conditions:
- if a[k] = x, such 1 ≤ x ≤ n, operation k increase(x),
- if a[k] = n + 1 operation k max counter.
both conditions stated in code above. obviusly wrong confused, , not know how understand differently.
why code wrong, missing task description?
one of top rated answers looks this:
public int[] solution(int n, int[] a) { int[] result = new int[n]; int maximum = 0; int resetlimit = 0; (int k = 0; k < a.length; k++) { if (a[k] < 1 || a[k] > n + 1) throw new invalidoperationexception(); if (a[k] >= 1 && a[k] <= n) { if (result[a[k] - 1] < resetlimit) { result[a[k] - 1] = resetlimit + 1; } else { result[a[k] - 1]++; } if (result[a[k] - 1] > maximum) { maximum = result[a[k] - 1]; } } else { // inefficiency here //for (int = 0; < result.length; i++) // result[i] = maximum; resetlimit = maximum; } } (int = 0; < result.length; i++) result[i] = math.max(resetlimit, result[i]); return result; }
this code results 100% on codility.
question:
i know how author knew task use result[a[k] - 1]
? resetlimit
represent?
maybe misunderstood question due english not sure. can not go on it.
edit:
based on code provided, how did misunderstood assignment? asking explanation of problem. whether explain needs done, or take code correct result , provide , explanation why done way?
in opinion somehow mixed index of counter (values in a
) , value of counter (values in counter
). there no magic in using a[i]-1
- value x
problem description (adjusted 0-based index).
my naive approach be, way understood problem (i hope makes clear, code doing wrong):
public static int[] maxpart(int n, int[] a){ int[] counters = new int[n]; for(int = 0; < a.length; i++){ int x=a[i]; if(x>=1 && x<=n){ // encodes increment(x), x=a[i] counters [x-1] = counters [x-1] + 1; //-1, because our index 0-based } if(x == n + 1 ){// encodes setting counters max value int tmpmax = counters.max (); for(int h = 0; h < counters.length; h++){ counters [h] = tmpmax; } } } } return counters; }
clearly, slow complexity iso(n^2)
n=10^5
number of operations (length of array a
), in case of following operation sequence:
max counter, max counter, max counter, ....
the top rated solution solves problem in lazy manner , not update values explicitly every time max counter
operation encountered, remembers minimal value counters must have after operation in resetlimit
. thus, every time must increment counter, looks whether value must updated due former max counter
operations , makes max counter
operation didn't execute on counter
if(result[a[k] - 1] < resetlimit) { result[a[k] - 1] = resetlimit + 1; }
his solution runs in o(n)
, fast enough.
Comments
Post a Comment