Skip to content

Commit 0a82b29

Browse files
authored
Create 5959-minimum-operations-to-make-the-array-k-increasing.js
1 parent 7c68bf6 commit 0a82b29

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const kIncreasing = function(arr, k) {
7+
const n = arr.length
8+
const a = Array.from({ length: k }, () => Array())
9+
10+
for(let i = 0; i < k; i++) {
11+
for(let j = i; j < n; j += k) {
12+
a[i].push(arr[j])
13+
}
14+
}
15+
16+
let res = 0
17+
for(let i = 0; i < a.length; i++) {
18+
const r = a[i]
19+
res += r.length - lis(r)
20+
}
21+
22+
return res
23+
24+
function bisect_right(a, x, lo = 0, hi = null) { // > upper_bound
25+
if (lo < 0) throw new Error('lo must be non-negative');
26+
if (hi == null) hi = a.length;
27+
while (lo < hi) {
28+
let mid = parseInt((lo + hi) / 2);
29+
x < a[mid] ? hi = mid : lo = mid + 1;
30+
}
31+
return lo;
32+
}
33+
34+
function lis(ar) {
35+
let q = []
36+
for (let x of ar) {
37+
let i = bisect_right(q, x)
38+
if (i == q.length) q.push(x)
39+
else q[i] = x
40+
}
41+
42+
return q.length
43+
}
44+
};
45+

0 commit comments

Comments
 (0)