原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/

题目:

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.

You may assume the array's length is at most 10,000.

Example:

Input:
[1,2,3]
Output:
2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]

题解:

The median minimizes the sum of absolute deviations. There is an article explaining this.

用quick sort找到median. 类似Kth Largest Element in an Array.

每一个元素对于median差值的绝对值的总和就是最小moves. 这里的median可以理解为第nums.length/2小的element.

Time Complexity: O(n), quick select O(n). n = nums.length.

Space: O(1).

AC Java:

 class Solution {
public int minMoves2(int[] nums) {
if(nums == null || nums.length < 2){
return 0;
} int n = nums.length;
int median = findK(nums, (n - 1) / 2, 0, n - 1);
int res = 0;
for(int num : nums){
res += Math.abs(num - median);
} return res;
} private int findK(int [] nums, int k, int l, int r){
if(l >= r){
return nums[l];
} int m = partition(nums, l, r);
if(m == k){
return nums[k];
}else if(m < k){
return findK(nums, k, m + 1, r);
}else{
return findK(nums, k, l, m - 1);
}
} private int partition(int [] nums, int l, int r){
int pivot = nums[l];
while(l < r){
while(l < r && nums[r] >= pivot){
r--;
} nums[l] = nums[r]; while(l < r && nums[l] <= pivot){
l++;
} nums[r] = nums[l];
} nums[l] = pivot;
return l;
}
}

类似Minimum Moves to Equal Array ElementsBest Meeting Point.

LeetCode Minimum Moves to Equal Array Elements II的更多相关文章

  1. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  2. LeetCode Minimum Moves to Equal Array Elements

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...

  3. Leetcode-462 Minimum Moves to Equal Array Elements II

    #462.   Minimum Moves to Equal Array Elements II Given a non-empty integer array, find the minimum n ...

  4. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  5. 【LeetCode】462. Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  6. [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  7. [Swift]LeetCode462. 最少移动次数使数组元素相等 II | Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  8. 462. Minimum Moves to Equal Array Elements II

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  9. 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...

随机推荐

  1. hdu 3746 Cyclic Nacklace

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 思路:KMP中Next数组的应用,求出最小的循环节,题目的意思是只能在字符串的后面上添加新的字符 ...

  2. PHP实现四种基本排序算法

    前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思 ...

  3. 局部加权回归、欠拟合、过拟合(Locally Weighted Linear Regression、Underfitting、Overfitting)

    欠拟合.过拟合 如下图中三个拟合模型.第一个是一个线性模型,对训练数据拟合不够好,损失函数取值较大.如图中第二个模型,如果我们在线性模型上加一个新特征项,拟合结果就会好一些.图中第三个是一个包含5阶多 ...

  4. Android中Input型输入设备驱动原理分析(一)

    转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...

  5. Intellij IDEA中的Mybatis Plugin破解

    具体的破解过程请看:https://github.com/luyanliang/profile/blob/master/idea/plugin/MybatisPlugin/Mybatis-Plugin ...

  6. 搭建高可用MongoDB集群(一):配置MongoDB

    在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQL有这些优势: 大数据量,可以通过 ...

  7. abort 用法讨论

    同事说:  TT***.factary(  procedure()  begin    ....    abort;    ....  end)大家在TTask中不建议用abort因为难以控制,我测试 ...

  8. windows 2008 server NTP Server

    1. 选择一台服务器作为时间同步服务器. 2. 运行Regedit,打开注册表编辑器. 3. 找到注册表项HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Ser ...

  9. Mac iTerm 很好用的终端

    配合Go2Shell使用,效果更佳!我建议你去商店下载它! 先去官网下载软件, 然后可以看看这一篇简明教程. 这里,还有一点点干货也一起带走吧! open -a Go2Shell --args con ...

  10. AJAX实现跨域的三种方法

    由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问. 比如说你的网站域名是aaa.com,想要通过AJAX请 ...