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 ...
随机推荐
- Ubuntu 出现 Invalid operation update 或 Invalid operation upgrade的解决办法
输入 sudo apt update && sudo apt full-upgrade
- 利用ant 执行jmeter用例生成html格式报告
1.安装ant 2.准备jmeter 及用例文件.jmx 3.编辑ant 执行文件build.xml <?xml version="1.0" encoding="G ...
- shell基础之二 bash特性详解
https://blog.51cto.com/13520779/2093146 合格linux运维人员必会的30道shell编程面试题及讲解:https://blog.51cto.com/oldboy ...
- 0 - Visualizing and Understanding Convolutional Networks(阅读翻译)
卷积神经网络的可视化理解(Visualizing and Understanding Convolutional Networks) 摘要(Abstract) 近来,大型的卷积神经网络模型在Image ...
- Java后端开发规范
基于阿里巴巴JAVA开发规范整理 一.命名风格 [强制]类名使用 UpperCamelCase 风格,必须遵从驼峰形式,但以下情形例外:DO / BO / DTO / VO / AO 正例:Marco ...
- InnoDB缓存---InnoDB Buffer Pool
InnoDB Buffer Pool 定义 对于InnoDB存储引擎,不管用户数据还是系统数据都是以页的形式存储在表空间进行管理的,其实都是存储在磁盘上的. 当InnoDB处理客户端请求,需要读取某页 ...
- Java通过过滤器修改header
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器
TIZ_c 第0周总结(2019/10/15-2019/10/22)工欲善其事必先利其器 任务清单 给自己取一个酷酷的id,并选择1-2个喜欢的方向.(只是初步选择,后期可更改) 改下群名片.例如yo ...
- Vue入门下
使用npm创建项目,系统会自动生成一些列文件. 以慕课网上的Travel项目来说,在生成的项目文件中存在src文件夹,这个文件夹也是平时在做项目的时候用的比较多的,其他的一些配置信息更改的频率较低. ...
- QList和QVector等容器的区别
QList和QVector等容器的区别. 1.大多数情况下可以用QList.像prepend()和insert()这种操作,通常QList比QVector快的多.这是因为QList是基于index标签 ...