-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path2111-minimum-operations-to-make-the-array-k-increasing.js
87 lines (74 loc) · 1.72 KB
/
2111-minimum-operations-to-make-the-array-k-increasing.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
85
86
/**
* @param {number[]} arr
* @param {number} k
* @return {number}
*/
const kIncreasing = function(arr, k) {
let res = 0, matrix = Array.from({ length: k }, () => []), n = arr.length
for(let i = 0; i < k; i++) {
for(let j = i; j < n; j += k) {
matrix[i].push(arr[j])
}
}
for (let i = 0; i < k; i++) {
res += matrix[i].length - nonDecreasing(matrix[i])
}
return res
function bisect_right(ar, x, l = 0, r) {
if(r == null) r = ar.length
while(l < r) {
const mid = ~~((l + r) / 2)
if(ar[mid] <= x) l = mid + 1
else r = mid
}
return l
}
function nonDecreasing(ar) {
let stk = []
for(let e of ar) {
const idx = bisect_right(stk, e)
if(idx === stk.length) stk.push(e)
else stk[idx] = e
}
return stk.length
}
};
// another
/**
* @param {number[]} arr
* @param {number} k
* @return {number}
*/
const kIncreasing = function(arr, k) {
const n = arr.length
const a = Array.from({ length: k }, () => Array())
for(let i = 0; i < k; i++) {
for(let j = i; j < n; j += k) {
a[i].push(arr[j])
}
}
let res = 0
for(let i = 0; i < a.length; i++) {
const r = a[i]
res += r.length - lis(r)
}
return res
function bisect_right(a, x, lo = 0, hi = null) { // > upper_bound
if (lo < 0) throw new Error('lo must be non-negative');
if (hi == null) hi = a.length;
while (lo < hi) {
let mid = parseInt((lo + hi) / 2);
x < a[mid] ? hi = mid : lo = mid + 1;
}
return lo;
}
function lis(ar) {
let q = []
for (let x of ar) {
let i = bisect_right(q, x)
if (i == q.length) q.push(x)
else q[i] = x
}
return q.length
}
};