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 ...
随机推荐
- Luogu P4109 [HEOI2015]定价 贪心
思路:找规律?$or$贪心. 提交:1次 题解: 发现:若可以构成$X0000$,答案绝对不会再在数字最后把$0$改成其他数: 若可以构成$XX50...0$更优. 所以左端点增加的步长是增加的($i ...
- PHP mysqli_multi_query() 函数
实例 执行多个针对数据库的查询: <?php $con=mysqli_connect("localhost","my_user","my_pas ...
- Java进阶知识14 Struts2中的S标签
1.A 开头 <s:a href=""></s:a> //超链接,类似于html里的<a></a> <s:action nam ...
- .NetCore 读取配置文件
1.创建config.json配置,并设置成始终复制 2.需要安装 nuget 包 Microsoft.Extensions.Configuration .Microsoft.Extensions.C ...
- SpringMVC——数据乱码问题
乱码解决: 1.controller传递数据给页面 :在RequestMapping当中指定produces="text/json;charset=utf-8" 2.Control ...
- Appium基础教程
目录 Appium教程 Appium简介 App自动化测试工具对比 Appium实现原理 环境搭建 Andorid介绍 基本架构 常见布局/视图 基本控件 控件常见属性 Adb介绍 Adb常用命令 A ...
- 在linux操作系统上进行简单的C语言源码的gcc编译实验
尝试在linux上用gcc 而非封装完好的codeblocks,vs等ide 来编译c和cpp源程序 首先查看我的gcc版本,我的是VM centos 自带的,没有的话得自行安装,安装上gcc就可以在 ...
- 理解TCP三次握手和四次挥手
TCP相关知识 TCP是面向连接的传输层协议,它提供可靠交付的.全双工的.面向字节流的点对点服务.HTTP协议便是基于TCP协议实现的.(虽然作为应用层协议,HTTP协议并没有明确要求必须使用TCP协 ...
- POJ 1741 Tree ——(树分治)
思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...
- UVA 796 Critical Links —— (求割边(桥))
和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混 ...