首页 > 其他分享 >LeetCode 2263. Make Array Non-decreasing or Non-increasing

LeetCode 2263. Make Array Non-decreasing or Non-increasing

时间:2024-07-15 23:40:21浏览次数:18  
标签:Non nums int res Make non decreasing increasing

原题链接在这里:https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing/description/

题目:

You are given a 0-indexed integer array nums. In one operation, you can:

  • Choose an index i in the range 0 <= i < nums.length
  • Set nums[i] to nums[i] + 1 or nums[i] - 1

Return the minimum number of operations to make nums non-decreasing or non-increasing.

 

Example 1:

Input: nums = [3,2,4,5,0]
Output: 4
Explanation:
One possible way to turn nums into non-increasing order is to:
- Add 1 to nums[1] once so that it becomes 3.
- Subtract 1 from nums[2] once so it becomes 3.
- Subtract 1 from nums[3] twice so it becomes 3.
After doing the 4 operations, nums becomes [3,3,3,3,0] which is in non-increasing order.
Note that it is also possible to turn nums into [4,4,4,4,0] in 4 operations.
It can be proven that 4 is the minimum number of operations needed.

Example 2:

Input: nums = [2,2,3,4]
Output: 0
Explanation: nums is already in non-decreasing order, so no operations are needed and we return 0.

Example 3:

Input: nums = [0]
Output: 0
Explanation: nums is already in non-decreasing order, so no operations are needed and we return 0. 

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000

Follow up: Can you solve it in O(n*log(n)) time complexity?

题解:For non-increasing is equal to reverse num with non-decreasing, thus we only need to think about non-decreasing going from left to right.

For an array, e.g. 2, 6, 8, 5. We need minimum move 3 to make 8 and 5 equal. they could be [5, 5], [6, 6], [7, 7] or [8, 8]. Thus this bundle range is [5, 8] with 3 moves.

We choose lowest bound 5 as it is easy for the new number. But the previous number is 6. Actually that is a range, thus we know it is no charge to bump it. 

Now array is 2, 6, 5, 5 with 3 moves.

If we have a new number 4, now we need to using the current maximum 6 to bundle with new 4. It takes at least 2 moves and move bundle range from 4 to 6.

We also keeps the minimum range 4 as bundle.
Now array is 2, 4, 5, 5, 4.

Eventually we get the minimum move is 5. The acutally array could be 2, 5, 5, 5, 5.

Time Complexity: O(n * logn). n = nums.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int convertArray(int[] nums) {
 3         int res = minMove(nums);
 4         reverse(nums);
 5         res = Math.min(res, minMove(nums));
 6         return res;
 7     }
 8 
 9     private int minMove(int[] nums){
10         PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
11         int res = 0;
12         for(int num : nums){
13             if(!maxHeap.isEmpty() && maxHeap.peek() > num){
14                 res += maxHeap.poll() - num;
15                 maxHeap.add(num);
16             }
17 
18             maxHeap.add(num);
19         }
20 
21         return res;
22     }
23 
24     private void reverse(int[] nums){
25         int l = 0;
26         int r = nums.length - 1;
27         while(l < r){
28             int temp = nums[l];
29             nums[l] = nums[r];
30             nums[r] = temp;
31             l++;
32             r--;
33         }
34     }
35 }

 

标签:Non,nums,int,res,Make,non,decreasing,increasing
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18304251

相关文章

  • 9. 嵌套的 CMake
    9.嵌套的CMake如果项目很大,或者项目中有很多的源码目录,在通过CMake管理项目的时候如果只使用一个CMakeLists.txt,那么这个文件相对会比较复杂,有一种化繁为简的方式就是给每个源码目录都添加一个CMakeLists.txt文件(头文件目录不需要),这样每个文件都不会太复杂,而且更灵活,更容......
  • 2. CMake 的简单使用
    2.CMake的简单使用我们创建一个工程目录,在里面定义一些简单的加减乘除运算,然后定义一个main.cpp的文件:结构如下:tree/f.\D:\SOURCE\CMAKE_PROJ└─proj1add.cppCMakeLists.txtdiv.cpphead.hmain.cppmul.cpp......
  • 1. CMake 概述
    1.CMake概述CMake可以用来构建C/C++工程,可以跨平台。允许开发者指定整个工程的编译流程在CMake没有出来之前,开发者需要手写makefile,但是不同平台下的makefile写法不同,所以移植软件的难度就很大。而CMake可以自动生成本地化的工程文件和makefile,其编译流程如下:蓝色......
  • Debug Log - Linux下出现 cmake: command not found
    Bug情况:在用脚本安装一些环境时,出现了cmake:commandnotfound的情况,故需要安装cmake。踩坑:网上有人说通过yum来安装cmake,但我先通过apt安装yum(sudoaptinstallyum),再通过yum安装cmake(sudoyuminstallcmake),发现yum找不到对应匹配的包。解决过程:使用cmake--version......
  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • Fatal error: Call to a member function read() on a non-object in 错误解决方法(织
    大家都说这是因为织梦代码优化不好怎么着怎么着的,其实有一些是因为这个原因,但不是完全因为这个。dede登录后台卡死原因分析登录完后台,加载的分别为顶部、左侧、右侧内容三个部分。顶部只是简单的查询一下权限不会卡、左侧也是简单的查询了一下也不会卡,那么原因就是......
  • ARMv8中non-shareable inner-shareable outer-shareable属性
    如果将block的内存属性配置成Non-cacheable,那么数据就不会被缓存到cache,那么所有observer看到的内存是一致的,也就说此时也相当于OuterShareable。其实官方文档,也有这一句的描述:在B2.7.2章节“Dataaccessestomemorylocationsarecoherentforallobserversinthesys......
  • QT6 CMake项目配置 (VSCode)
    QT6CMake项目配置(VSCode)这篇文章我们介绍一下在VSCode下的配置,大体上和VisualStudio上差不多,建议先把之前介绍在VS上的配置过程看一遍,VSCode安装这个就不用说了吧,无脑下一步插件安装先把CMake相关的插件装一下第一个是CMake语言的支持插件,装了这个写CMakeLists.txt就......
  • remake 前的训练记录
    2024.7.9cf1989赛时通过abcd,补了e。E对于原数组的一段元素相同的区间,会对应到b数组形如\([1,2,\cdots,x-1,x,x,x-1,\cdots,2,1]\)或者\([1,2,\cdots,x-1,x,x-1,\cdots,2,1]\)的区间。所以只需要求长度为\(n\)的序列能被切成至少\(k\)段的方......
  • Fatal error: Call to a member function read() on a non-object in 错误解决方法(织
    大家都说这是因为织梦代码优化不好怎么着怎么着的,其实有一些是因为这个原因,但不是完全因为这个。dede登录后台卡死原因分析登录完后台,加载的分别为顶部、左侧、右侧内容三个部分。顶部只是简单的查询一下权限不会卡、左侧也是简单的查询了一下也不会卡,那么原因就是......