leetcode 5959. 使数组 K 递增的最少操作次数

 

 

 1 class Solution {
 2    
 3 public:
 4     int kIncreasing(vector<int>& arr, int k) {
 5         int n=arr.size();
 6         int st[n],top=0,ans=0,cnt=0;
 7         for(int i=0;i<k;i++)
 8         {
 9             top=0,cnt=0;
10             for(int j=i;j<n;j+=k)
11             {
12                 cnt++;
13                 if(!top||st[top-1]<=arr[j])st[top++]=arr[j];
14                 else
15                 {
16                    int t= upper_bound(st,st+top,arr[j])-st;
17                    st[t]=arr[j];
18                 }   
19             }
20             ans+=cnt-top;
21         }
22         return ans;
23     }
24 };

对每个k序列进行最长子序列操作;对于某个子序列的答案就是cnt-最长子序列长度;

posted @ 2021-12-19 14:30  matt-11  阅读(122)  评论(0)    收藏  举报