Skip to content

Instantly share code, notes, and snippets.

@Nditah
Created March 13, 2018 13:49
Show Gist options
  • Save Nditah/87348a60b494f1691d3e76226dacf444 to your computer and use it in GitHub Desktop.
Save Nditah/87348a60b494f1691d3e76226dacf444 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jiyonuj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*
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 zero-indexed 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.
For example, given integer N = 5 and array A such that:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
the values of the counters after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)
The goal is to calculate the value of every counter after all operations.
Write a function:
function solution(N, A);
that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.
*/
function solution(N=1, A=[]){
let counter = new Array(N);
counter.fill(0);
for(let M of A){
if( M > N) {
counter.fill(Math.max(...counter));
// counter.forEach(function(item, i) { counter[i] = maxC ; });
}
else{
if(counter[M-1] > 0){
counter[M-1] ++;
}else{
counter[M-1] = 1 ;
}
}
}
return counter ;
}
let A = [];
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
let N =5;
console.log(solution(N, A)); // [3, 2, 2, 4, 2]
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
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 zero-indexed 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.
For example, given integer N = 5 and array A such that:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
the values of the counters after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)
The goal is to calculate the value of every counter after all operations.
Write a function:
function solution(N, A);
that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.
*/
function solution(N=1, A=[]){
let counter = new Array(N);
counter.fill(0);
for(let M of A){
if( M > N) {
counter.fill(Math.max(...counter));
// counter.forEach(function(item, i) { counter[i] = maxC ; });
}
else{
if(counter[M-1] > 0){
counter[M-1] ++;
}else{
counter[M-1] = 1 ;
}
}
}
return counter ;
}
let A = [];
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
let N =5;
console.log(solution(N, A)); // [3, 2, 2, 4, 2]</script></body>
</html>
/*
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 zero-indexed 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.
For example, given integer N = 5 and array A such that:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
the values of the counters after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)
The goal is to calculate the value of every counter after all operations.
Write a function:
function solution(N, A);
that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.
*/
function solution(N=1, A=[]){
let counter = new Array(N);
counter.fill(0);
for(let M of A){
if( M > N) {
counter.fill(Math.max(...counter));
// counter.forEach(function(item, i) { counter[i] = maxC ; });
}
else{
if(counter[M-1] > 0){
counter[M-1] ++;
}else{
counter[M-1] = 1 ;
}
}
}
return counter ;
}
let A = [];
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
let N =5;
console.log(solution(N, A)); // [3, 2, 2, 4, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment