-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_problems.js
More file actions
80 lines (72 loc) · 1.97 KB
/
Copy pathcount_problems.js
File metadata and controls
80 lines (72 loc) · 1.97 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// frog river one
function solution(X,A){
const AL = A.length;
const positionSet = new Set();
for (let i = 0; i < AL; i++){
positionSet.add(A[i]);
if(positionSet.size === X){
return i;
}
}
return -1;
}
//permCheck
function solution(A){
const setCheck = new Set(A);
// const n = A.length;
// // 计算预期的等差数列和(从 1 到 n)
// const expectedSum = (n * (Math.min(...A) + Math.max(...A))) / 2;
// const actualSum = A.reduce((sum,num) =>{sum + num}, 0);
// if ( setCheck.size === A.length && expectedSum === actualSum &&Math.max(...A) === n && Math.min(...A) === 1){
// return 1;
// }
const n = A.length;
if (setCheck.size === n && Math.max(...A) === n && Math.min(...A) === 1) {
return 1;
} else {
return 0;
}
return 0;
}
//maxCounters
function solution(N,A){
let result = new Array(N).fill(0);
let maxCounter =0;
let lazyMax = 0;
for(let i =0; i < A.length; i++){
if(A[i] === N +1){
result = result.map(() => maxCounter);
// maxCounter = Math.max(lazyMax,maxCounter);
} else if (1 <= A[i] && A[i]<= N){
result[A[i]-1] += 1;
maxCounter = Math.max(result[A[i] -1], maxCounter);
}
}
return result;
}
//Missing Integer(brute force)
function solution(A){
let misInt = 1;
let maxInt = -1;
const removeNeg = A.filter(x => x > 0);
//Missing Integer(brute force)
function solution(A){
let misInt = 1;
let maxInt = -1;
const removeNeg = A.filter(x => x >= 0);
// const removeRepeat = new Set(A);
if (removeNeg.length > 1){
for( let i = 0; i < removeNeg.length -1; i++){
if(removeNeg[i] <= removeNeg[i+1]){
maxInt = Math.max(maxInt,removeNeg[i+1]);
misInt = maxInt + 1;
} else {
maxInt = removeNeg[i];
misInt = maxInt + 1;
}
}
}else if (removeNeg.length === 1 ){
misInt = removeNeg[0] + 1;
}
return misInt;
}