-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad46a88
commit b43a146
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# [MaxCounters](https://codility.com/programmers/lessons/4-counting_elements/) | ||
Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum. | ||
|
||
### Solution (JavaScript) | ||
To solve this, we know that we need to loop through all of A, in order to find each operation we must apply. We can keep track of the new minimum and only add it to an element when we go to change it. Adding a final loop at the end to ensure everything is greater or equal to the minimum. | ||
|
||
__[Test Score: 100%](https://codility.com/demo/results/trainingNN3Z7Q-9DN/)__ | ||
|
||
```js | ||
function solution(N, A) { | ||
let B = new Array(N).fill(0) | ||
|
||
let minValue = 0 | ||
let maxValue = 0 | ||
for (let i = 0; i < A.length; ++i){ | ||
if ( A[i] > N ) { | ||
//then new base line is needed | ||
minValue = maxValue | ||
} else { | ||
// check if less than the min | ||
let j = A[i] -1 | ||
if ( B[j] < minValue ) B[j] = minValue | ||
// increase by one and check if we have a new max | ||
B[j]++ | ||
if ( B[j] > maxValue ) maxValue = B[j] | ||
} | ||
} | ||
// one final loop through B to check nothing is less than the min | ||
for (let i = 0; i < B.length; ++i){ | ||
if ( B[i] < minValue ) B[i] = minValue | ||
} | ||
|
||
return B | ||
} | ||
``` |