Min swaps to sort array
Given an array with distinct numbers, return an integer indicating the minimum number of swap operations required to sort the array into ascending order.
Example 1:
Input: [5, 1, 3, 2]
Output: 2
Explanation: [5, 1, 3, 2] -> [2, 1, 3, 5] -> [1, 2, 3, 5]
Example 2:
Input: [1, 3, 2]
Output: 1
Explanation: [1, 3, 2] -> [1, 2, 3]
分析:
先把原数组保存一份,然后对另一份排序,并且把原数组的值与index保存一份。这样,我们需要得到某个数的位置的时候,直接通过map得到。
public class Solution {
public int minSwaps(int[] elems) {
int counter = ;
Map<Integer, Integer> map = buildMap(elems);
int[] copy = new int[elems.length];
System.arraycopy(elems, , copy, , elems.length);
Arrays.sort(elems);
for (int i = ; i < copy.length; i++) {
if (copy[i] != elems[i]) {
swap(copy, i, map.get(elems[i]), map);
counter++;
}
}
return counter;
}
private Map<Integer, Integer> buildMap(int[] elems) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = ; i < elems.length; i++) {
map.put(elems[i], i);
}
return map;
}
private void swap(int[] elems, int i, int j, Map<Integer, Integer> map) {
map.put(elems[j], i);
map.put(elems[i], j);
int temp = elems[j];
elems[j] = elems[i];
elems[i] = temp;
}
}
Min swaps to sort array的更多相关文章
- js & sort array object
js & sort array object sort array object in js https://flaviocopes.com/how-to-sort-array-of-obje ...
- 42.旋转数组的最小元素[Get min value of rotated array]
[题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- Sort Array By Parity II LT922
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- Relative Sort Array
Relative Sort Array Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all eleme ...
随机推荐
- 在chrome开发者模式中查找你的js文件
在chrom开发者模式中按ctrl+o查找你的js文件
- kubernetes 的数据的存储 存储卷
根据应用本身是否 需要持久存储数据,以及某一此请求和此前的请求是否有关联性,可以分为四类应用: 1.有状态要存储 2.有状态无持久存储 3.无状态无持久存储4.无状态有持久存储 在k8s上的数据持久性 ...
- Codeforces 915E. Physical Education Lessons(动态开点线段树)
E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...
- Vue小实例
最近刚学习Vue的官方文档,了解了指令.模板.组件.数据双向绑定等有关Vue的知识点.因此估摸着做点实例出来练练手. 下面介绍一个简单的例子,模拟购物车自动统计金额,效果图如下: 代码如下: < ...
- redis快照关闭了导致不能持久化的问题
在使用redis的时候我们经常会遇到这种bug: Python与Redis交互时,设置数据出现下列报错信息: MISCONF Redis is configured to save RDB s ...
- Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee Linux上tomcat无法正常启动
上传了个tomcat7的压缩包上linux服务器,解压后,想直接启动,发现报错: Cannot find ./catalina.sh The file is absent or does not ha ...
- Centos7 yum安装mysql(完整版)
1.下载mysql 地址:https://dev.mysql.com/downloads/repo/yum/.选择对应版本下载.
- [Java]使用Collections.Max,Min方法取集合类的最大最小值
代码: package com.hy; import java.util.Arrays; import java.util.Collections; import java.util.List; pu ...
- adb、pm命令操作apk包
1.adb shell pm list package 打印出来所有安装到手机上的APP包名 2.adb shell pm path com.xxx.xxx 找出安装后的包名应用的apk所在位置 3. ...
- 服务器开启FTP功能
介绍几个比较完整的教程链接 Windows Server 2012 之文件服务器(FTP)