Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 458edeb

Browse files
committed
Add solution #2111
1 parent bede0a0 commit 458edeb

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,754 LeetCode solutions in JavaScript
1+
# 1,755 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1617,6 +1617,7 @@
16171617
2108|[Find First Palindromic String in the Array](./solutions/2108-find-first-palindromic-string-in-the-array.js)|Easy|
16181618
2109|[Adding Spaces to a String](./solutions/2109-adding-spaces-to-a-string.js)|Medium|
16191619
2110|[Number of Smooth Descent Periods of a Stock](./solutions/2110-number-of-smooth-descent-periods-of-a-stock.js)|Medium|
1620+
2111|[Minimum Operations to Make the Array K-Increasing](./solutions/2111-minimum-operations-to-make-the-array-k-increasing.js)|Hard|
16201621
2114|[Maximum Number of Words Found in Sentences](./solutions/2114-maximum-number-of-words-found-in-sentences.js)|Easy|
16211622
2115|[Find All Possible Recipes from Given Supplies](./solutions/2115-find-all-possible-recipes-from-given-supplies.js)|Medium|
16221623
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* 2111. Minimum Operations to Make the Array K-Increasing
3+
* https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k.
7+
*
8+
* The array arr is called K-increasing if arr[i-k] <= arr[i] holds for every index i, where
9+
* k <= i <= n-1.
10+
*
11+
* - For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because:
12+
* - arr[0] <= arr[2] (4 <= 5)
13+
* - arr[1] <= arr[3] (1 <= 2)
14+
* - arr[2] <= arr[4] (5 <= 6)
15+
* - arr[3] <= arr[5] (2 <= 2)
16+
* - However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because
17+
* arr[0] > arr[3]).
18+
*
19+
* In one operation, you can choose an index i and change arr[i] into any positive integer.
20+
*
21+
* Return the minimum number of operations required to make the array K-increasing for the given k.
22+
*/
23+
24+
/**
25+
* @param {number[]} arr
26+
* @param {number} k
27+
* @return {number}
28+
*/
29+
var kIncreasing = function(arr, k) {
30+
let result = 0;
31+
for (let i = 0; i < k; i++) {
32+
const subsequence = [];
33+
for (let j = i; j < arr.length; j += k) {
34+
subsequence.push(arr[j]);
35+
}
36+
result += longestNonDecreasingSubsequence(subsequence);
37+
}
38+
39+
return result;
40+
41+
function longestNonDecreasingSubsequence(nums) {
42+
const tails = [];
43+
for (const num of nums) {
44+
let left = 0;
45+
let right = tails.length;
46+
while (left < right) {
47+
const mid = Math.floor((left + right) / 2);
48+
if (tails[mid] <= num) {
49+
left = mid + 1;
50+
} else {
51+
right = mid;
52+
}
53+
}
54+
tails[left] = num;
55+
}
56+
return nums.length - tails.length;
57+
}
58+
};

0 commit comments

Comments
 (0)