-
Notifications
You must be signed in to change notification settings - Fork 0
/
countingElements.js
51 lines (46 loc) · 1.25 KB
/
countingElements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//FrogRiverOne
function solution(X, A) {
const counts = new Map();
for (let i = 0; i < A.length; i++) {
let positon = A[i]
//check if position as a key in counts is not already exist -> add to the counts with key=position and value=true
if (!counts.has(positon)) {
counts.set(positon, true)
}
if (counts.size === X) {
return i
}
}
return -1
}
//PermCheck
function solution(A) {
let expectedElement = 0;
A.sort((a,b) => {return a-b})
for (let i=0; i<A.length; i++){
if (A[i] === expectedElement+1){
expectedElement++
}
}
return (expectedElement === A.length ? 1 : 0)
}
//MaxCounter
function solution(N, A) {
let counters = [...Array(N)].map(x => 0);
let max = 0;
let lastMax = 0;
for(j=0; j < A.length; j++){
if(A[j] < N+1){
i = A[j] - 1;
if (counters[i] < lastMax) counters[i] = lastMax;
counters[i]++;
if (max < counters[i]) max = counters[i];
} else {
lastMax = max;
}
}
for(j = 0; j < N; j++){
if (counters[j] < lastMax) counters[j] = lastMax;
}
return counters;
}