-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
84 lines (75 loc) · 1.44 KB
/
util.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
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
81
82
83
84
// prettier-ignore
if (typeof _ === 'undefined') {
_ = require('lodash');
}
// map utils
const mapPush = (map, key, value) => {
if (map[key]) {
map[key].push(value);
} else {
map[key] = [value];
}
};
const mapAdd = (map, key) => {
if (map[key]) {
map[key] = map[key] + 1;
} else {
map[key] = 1;
}
};
// Remove
Array.prototype.remove = function (val) {
const index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
return this;
};
const mod = Math.pow(10, 9) + 7;
console.log(Number.MAX_SAFE_INTEGER);
console.log(Number.MAX_VALUE);
// set equal
const isSetsEqual = (a, b) =>
a.size === b.size && [...a].every((value) => b.has(value));
function permute(permutation) {
var length = permutation.length,
result = [permutation.slice()],
c = new Array(length).fill(0),
i = 1,
k,
p;
while (i < length) {
if (c[i] < i) {
k = i % 2 && c[i];
p = permutation[i];
permutation[i] = permutation[k];
permutation[k] = p;
++c[i];
i = 1;
result.push(permutation.slice());
} else {
c[i] = 0;
++i;
}
}
return result;
}
const minSwapAdj = (s1, s2) => {
let i = 0;
let j = 0;
let result = 0;
while (i < s1.length) {
while (s1[j] !== s2[i]) {
j += 1;
}
while (i < j) {
let t = s1[j];
s1[j] = s1[j - 1];
s1[j - 1] = t;
j -= 1;
result++;
}
i += 1;
}
return result;
};