LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里: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 Elements, Best Meeting Point.
LeetCode Minimum Moves to Equal Array Elements II的更多相关文章
- [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 ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- 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 ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【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 ...
- [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 ...
- [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 ...
- 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 ...
- 462 Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等 II
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000.例如:输入:[1,2,3]输出:2说明:只有两个动作是必 ...
随机推荐
- Android 某些配置记录
1, system image 大小配置: devices/intel/baytrail/ffrd8/BoardConfig.mk : BOARD_SYSTEMIMAGE_PARTITION_SIZ ...
- SQL优化技巧
我们开发的大部分软件,其基本业务流程都是:采集数据→将数据存储到数据库中→根据业务需求查询相应数据→对数据进行处理→传给前台展示.对整个流程进行分析,可以发现软件大部分的操作时间消耗都花在了数据库相关 ...
- Java中分割字符串
java.lang.String 的 split() 方法, JDK 1.4 or later public String[] split(String regex,int limit) 示例代码 p ...
- Oracle 11g 新特性之Highly Available IP(HAIP)
Redundant Interconnect with Highly Available IP (HAIP) 简介 从11.2.0.2开始,Oracle 的集群软件Grid Infrastruct ...
- Mac下的Maven配置
1.确保电脑已经安装java,并配置JAVA_HOME环境变量 2.从官网下载Maven压缩包并解压,zip或者其他格式的都行 3.进入终端,编辑环境变量配置文件 vi .bash_profile 可 ...
- 验证启用了不安全的HTTP方法
安全风险: 可能会在Web 服务器上上载.修改或删除Web 页面.脚本和文件. 可能原因: Web 服务器或应用程序服务器是以不安全的方式配置的. 修订建议: 如果 ...
- Linux学习笔记(4)-远程登录
根据网上的那些说法,如Linux服务器假设在外地(新疆),和程序员工作的环境(北京)相距太远,那么每次出问题都要出差跑到现场去调试的话,那就太烦人了. 所以,人们开发出了一种远程登录的手段,可以让程序 ...
- bootstrap 使用需注意的一些点
table 中td的宽度可以td 的style设置,然后在设置内部比如img对象款高实现对于 table其中某列的设置.
- TScrollBox的用法 滚动事件
//滚轮事件:ScrollBox1: TScrollBox; procedure TfrmReleateGQAccount.ScrollBox1MouseWheel(Sender: TObject; ...
- POJ 3278 题解
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 78114 Accepted: 24667 ...