LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

这篇具有很好参考价值的文章主要介绍了LeetCode2111. 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文章来源地址https://www.toymoban.com/news/detail-837458.html

二、题解

class Solution {
public:
    int nums[100010];
    int ends[100010];
    int kIncreasing(vector<int>& arr, int k) {
        int n = arr.size();
        int res = 0;
        for(int i = 0;i < k;i++){
            int size = 0;
            for(int j = i;j < n;j += k){
                nums[size++] = arr[j];
            }
            res += size - lengthOfLIS(size);
        }
        return res;
    }
    int lengthOfLIS(int size) {
        int len = 0;
        for(int i = 0;i < size;i++){
            int index = binarySearch(len,nums[i]);
            if(index == -1) ends[len++] = nums[i];
            else ends[index] = nums[i];
        }
        return len;
    }
    int binarySearch(int len,int num){
        int l = 0, r = len - 1,res = -1;
        while(l <= r){
            int mid = (l + r) / 2;
            if(ends[mid] > num){
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        return res;
    }
};

到了这里,关于LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n

    2024年02月06日
    浏览(35)
  • 【Python】成功解决ValueError: zero-size array to reduction operation minimum which has no identity

    【Python】成功解决ValueError: zero-size array to reduction operation minimum which has no identity 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分9

    2024年04月16日
    浏览(55)
  • LeetCode452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons. Arrows can be shot up directly vertically (in

    2024年02月04日
    浏览(41)
  • leetcode - 1326. Minimum Number of Taps to Open to Water a Garden

    There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, …, n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i

    2024年02月10日
    浏览(37)
  • Leetcode 3016. Minimum Number of Pushes to Type Word II

    Leetcode 3016. Minimum Number of Pushes to Type Word II 1. 解题思路 2. 代码实现 题目链接:3016. Minimum Number of Pushes to Type Word II 这道题的话思路其实还是蛮简单的,显然我们的目的是要令对给定的word在键盘上敲击的次数最小。 因此,我们只需要对单词当中按照字符的频次进行倒序排列,

    2024年01月22日
    浏览(46)
  • LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [ x s t a r t , x e n d x_{start}, x_{end} x s t a r t ​ , x e n d ​ ] denotes a balloon whose horizontal diameter stretches between x s t a r t x_{start} x s t a r t ​ and x e n d x_{end} x

    2024年02月12日
    浏览(43)
  • LeetCode 1000. Minimum Cost to Merge Stones【记忆化搜索,动态规划,数组】困难

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月26日
    浏览(99)
  • 已解决To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags

    已解决WARNING:tensorflow:From stdin 1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version. Instructions for updating: Use tf.config.list_physical_devices(‘GPU’)~ instead. 2023-03-31 16:58:07.971004: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized

    2024年02月04日
    浏览(39)
  • vue 组件 import make sure to provide the “name“ option.

    百度了好多结果,都过时了,例如: 模块引入是否加{} 再比如: 对于递归组件,请确保提供“name”选项。 出现该错误情况之一: 错误由未正确引入组件或子组件引起,如element-ui中form表单组件未引用el-form-item子组件就会出现这个错误。 el-form       el-form-item        

    2024年02月02日
    浏览(40)
  • LeetCode //2675. Array of Objects to Matrix (Day 19 of LC JavaScript Challenge)

    Write a function that converts an array of objects arr into a matrix m. arr is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values. The first row m should be the column names. If there is no nesting, the column names are the unique keys

    2024年02月06日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包