Minimum Operations to Make the Array K-Increasing

You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k.

The array arr is called K-increasing if arr[i-k] <= arr[i] holds for every index i, where k <= i <= n-1.

  • For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because:
    • arr[0] <= arr[2] (4 <= 5)
    • arr[1] <= arr[3] (1 <= 2)
    • arr[2] <= arr[4] (5 <= 6)
    • arr[3] <= arr[5] (2 <= 2)
  • However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because arr[0] > arr[3]).

In one operation, you can choose an index i and change arr[i] into any positive integer.

Return the minimum number of operations required to make the array K-increasing for the given k.

Example 1:

Input: arr = [5,4,3,2,1], k = 1
Output: 4
Explanation:
For k = 1, the resultant array has to be non-decreasing.
Some of the K-increasing arrays that can be formed are [5,6,7,8,9], [1,1,1,1,1], [2,2,3,4,4]. All of them require 4 operations.
It is suboptimal to change the array to, for example, [6,7,8,9,10] because it would take 5 operations.
It can be shown that we cannot make the array K-increasing in less than 4 operations.

Example 2:

Input: arr = [4,1,5,2,6,2], k = 2
Output: 0
Explanation:
This is the same example as the one in the problem description.
Here, for every index i where 2 <= i <= 5, arr[i-2] <= arr[i].
Since the given array is already K-increasing, we do not need to perform any operations.

Example 3:

Input: arr = [4,1,5,2,6,2], k = 3
Output: 2
Explanation:
Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.
One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.
The array will now be [4,1,5,4,6,5].
Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.

Constraints:

  • 1 <= arr.length <= 105
  • 1 <= arr[i], k <= arr.length

思路:这题是 Longest Increasing Subsequence 的变种,

  • If k = 1, we need to find the minimum number of operations to make whole array non-decreasing.
  • If k = 2, we need to make:
    • newArr1: Elements in index [0, 2, 4, 6...] are non-decreasing.
    • newArr2: Elements in index [1, 3, 5, 7...] are non-decreasing.
  • If k = 3, we need to make:
    • newArr1: Elements in index [0, 3, 6, 9...] are non-decreasing.
    • newArr2: Elements in index [1, 4, 7, 10...] are non-decreasing.
    • newArr3: Elements in index [2, 5, 8, 11...] are non-decreasing.
  • Since Elements new arrays are independent, we just need to find the minimum of operations to make K new arrays are non-decreasing.
  • To find the minimum of operations to make an array are non-decreasing,
    • Firstly, we count Longest Non-Decreasing Subsequence in that array.
    • Then the result is len(arr) - longestNonDecreasing(arr), because we only need to change elements not in the Longest Non-Decreasing Subsequence.
    • For example: newArr = [18, 8, 8, 3, 9], the longest non-decreasing subsequence is [-, 8, 8, -, 9], so we just need to the array into [8, 8, 8, 9, 9] by changing 18 -> 83 -> 9.

To find the Longest Non-Decreasing Subsequence of an array, you can check following posts for more detail:

因为sub array是相互独立的,所以步骤就是每个sublist,找到LIS,然后用size - LIS就是这个array的minimum step;注意这题是等于也算递增的,跟LIS还是 有点不一样,要找序列中比自己大的那个元素的index,LIS是找第一个大于等于的index。

class Solution {
    public int kIncreasing(int[] arr, int k) {
        int n = arr.length;
        int res = 0;
        for(int i = 0; i < k; i++) {
            List<Integer> list = new ArrayList<>();
            for(int j = i; j < n; j += k) {
                list.add(arr[j]);
            }
            res += list.size() - getLIS(list);
        }
        return res;
    }
    
    private int getLIS(List<Integer> list) {
        List<Integer> tails = new ArrayList<>();
        tails.add(list.get(0));
        
        for(int i = 1; i < list.size(); i++) {
            int num = list.get(i);
            if (num >= tails.get(tails.size() - 1)) {
                tails.add(num);
            } else {
                tails.set(binarySearch(tails, 0, tails.size() - 1, num), num);
            }
        }
        return tails.size();
    }
    
    private int binarySearch(List<Integer> list, int start, int end, int num) {
        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(list.get(mid) <= num) {
                start = mid;
            } else {
                // A[mid] > num;
                end = mid;
            }
        }
        if(list.get(start) > num) {
            return start;
        } 
        return end;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值