Skip to content

Commit 4ed0b1d

Browse files
authored
Create 2111.Minimum-Operations-to-Make-the-Array-K-Increasing.cpp
1 parent d484570 commit 4ed0b1d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int kIncreasing(vector<int>& arr, int k)
4+
{
5+
int ret = 0;
6+
int n = arr.size();
7+
8+
for (int t=0; t<k; t++)
9+
{
10+
vector<int>nums;
11+
for (int i=t; i<n; i+=k)
12+
nums.push_back(arr[i]);
13+
ret += nums.size() - lengthOfLIS(nums);
14+
}
15+
16+
return ret;
17+
}
18+
19+
int lengthOfLIS(vector<int>& nums)
20+
{
21+
int n = nums.size();
22+
vector<int>q(n, INT_MAX);
23+
for (int i=0; i<n; i++)
24+
{
25+
auto iter = upper_bound(q.begin(),q.end(),nums[i]);
26+
*iter = nums[i];
27+
}
28+
for (int i = n - 1; i >= 0; i--)
29+
{
30+
if (q[i] != INT_MAX)
31+
return i + 1;
32+
}
33+
return 0;
34+
}
35+
};

0 commit comments

Comments
 (0)