首页 > 其他分享 >LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array

LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array

时间:2024-06-17 13:54:31浏览次数:24  
标签:elements Swaps nums int Make Minimum Swap array swaps

原题链接在这里:https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/

题目:

You are given a 0-indexed integer array nums.

Swaps of adjacent elements are able to be performed on nums.

A valid array meets the following conditions:

  • The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.
  • The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.

Return the minimum swaps required to make nums a valid array.

Example 1:

Input: nums = [3,4,5,5,3,1]
Output: 6
Explanation: Perform the following swaps:
- Swap 1: Swap the 3rd and 4th elements, nums is then [3,4,5,3,5,1].
- Swap 2: Swap the 4th and 5th elements, nums is then [3,4,5,3,1,5].
- Swap 3: Swap the 3rd and 4th elements, nums is then [3,4,5,1,3,5].
- Swap 4: Swap the 2nd and 3rd elements, nums is then [3,4,1,5,3,5].
- Swap 5: Swap the 1st and 2nd elements, nums is then [3,1,4,5,3,5].
- Swap 6: Swap the 0th and 1st elements, nums is then [1,3,4,5,3,5].
It can be shown that 6 swaps is the minimum swaps required to make a valid array.

Example 2:

Input: nums = [9]
Output: 0
Explanation: The array is already valid, so we return 0.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

题解:

List some examples and we can find the routine.

Find the right most max num ind and left most min num ind in case there are duplicates.

min swap =  max ind to the right edge + min ind to the left edge.

If the min ind > max Ind, we need to minus one since when we max to the right, we already swap once for the min.

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

Space: O(1).

AC Java:

 1 class Solution {
 2     public int minimumSwaps(int[] nums) {
 3         int n = nums.length;
 4         int maxInd = n - 1;
 5         int minInd = 0;
 6         for(int i = 0; i < n; i++){
 7             if(nums[minInd] > nums[i]){
 8                 minInd = i;
 9             }
10 
11             if(nums[maxInd] < nums[n - 1 - i]){
12                 maxInd = n - 1 - i;
13             }
14         }
15 
16         return n - 1 - maxInd + minInd - (minInd > maxInd ? 1 : 0);
17     }
18 }

 

标签:elements,Swaps,nums,int,Make,Minimum,Swap,array,swaps
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18252230

相关文章

  • CMake Tutorial (3.30-rc3版) 练习和点评
    CMakeTutorial练习和点评Author:ChrisZZTime:2024.06.1623:37:00CMake官方文档提供了CMakeTutorial,目前最新版是CMake-3.30-rc3,有12个Step供用户练习。CMakeTutorial是从CMake3.16版本开始能从官方网页找到,并且每一版都有改进Tutorial内容。作为有实际C/......
  • QtCreator CMakeLists.txt添加模块(Modules)
    修改以下位置,添加模块...set(CMAKE_CXX_STANDARD20)#设置C++标准#查找Qt6find_package(QTNAMESQt6Qt5REQUIREDCOMPONENTSWidgets**Multimedia**)find_package(Qt${QT_VERSION_MAJOR}REQUIREDCOMPONENTSWidgets**Multimedia**)...#链接Qt6模块和库target_l......
  • 209. Minimum Size Subarray Sum
    Givenanarrayofpositiveintegersnumsandapositiveintegertarget,returntheminimallengthofasubarraywhosesumisgreaterthanorequaltotarget.Ifthereisnosuchsubarray,return0instead.Example1:Input:target=7,nums=[2,3,1,2,......
  • Makefile手册中"+=",":=","?="操作符的区别
    目录Makefile手册中"+=",":=","?="操作符的区别1."?="操作符2."+="操作符3.":="操作符Makefile手册中"+=",":=","?="操作符的区别1."?="操作符在GNUmake中,有一个变量在之前没有被赋值的情况下才会对这个变量进行赋值的操作,被称为条件赋值操作......
  • Makefile Operation
    ########################################################################################filename:Makefile#author:[email protected]#data:2024/06/14#function:项目中采用Makefile#note:None#......
  • makefile和shell都怎么编写,举例说明
    Makefile和Shell脚本是两种不同的编程工具,它们各自有不同的用途和编写方式。MakefileMakefile是一种用于自动化编译的工具,它使用Makefile文件来定义编译规则。Makefile通常用于编译源代码,生成可执行文件或库文件。Makefile的基本结构包括目标(target)、依赖(dependencies)和命令(comm......
  • 编写一个.sh的脚本,然后通过 shell 脚本执行 Makefile 文件并把生成的可执行文件下载到
    要编写一个shell脚本来执行Makefile并下载生成的可执行文件到开发板,你需要确保开发板可以通过某种方式(如SSH、FTP、SCP等)访问。以下是一个简单的shell脚本示例,它使用scp命令将可执行文件从本地机器复制到开发板。假设你的开发板可以通过SSH访问,并且你已经配置了SSH密钥认证,这样你......
  • 编写一个 Makefile 文件,对阶段项目一的代码进行自动化编译
    为了编写一个Makefile文件来自动化编译一个项目,我们需要知道项目中包含哪些源文件以及它们是如何组织的。假设我们有一个简单的项目,它包含两个C源文件`main.c`和`helper.c`,以及一个头文件`helper.h`。我们希望编译这些文件生成一个名为`project`的可执行文件。以下是一个简单的M......
  • 简单的Makefile文件解析
    Makefile文件解析#commonmakefileheader#"$(变量)"、"$makefile内置变量"表示变量值DIR_INC=../../include #头文件相对路径DIR_BIN=../../bin #可执行文件的相对路径DIR_LIB=../../libs #库的相对路径TARGET =iat_online_record_sample #目标变量BIN_TA......
  • Oracle报错:“Error in invoking target ‘agent nmhs’ of makefile...”
    Oracle报错:“Errorininvokingtarget‘agentnmhs’ofmakefile...”  前言:Oracle在安装过程中的报错一定要重视,这决定你后续是否能完成安装以及是否能使用。我这边会陆续汇总一些报错现象以及解决方案共享。##InstallProduct86%报错信息:“Errorininvokingtarget'......