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: 2Explanation: Only two moves are needed (remember each move increments or decrements one element):[1,2,3] => [2,2,3] => [2,2,2]
题解:
用quick sort找到median. 类似Kth Largest Element in an Array. 每一个元素对于median差值的绝对值的总和就是最小moves. 这里的median可以理解为第nums.length/2小的element.
Time Complexity: O(n), quick sort O(n).
Space: O(1).
AC Java:
1 public class Solution { 2 public int minMoves2(int[] nums) { 3 int moves = 0; 4 int median = findK(nums, nums.length/2, 0, nums.length-1); 5 for(int num : nums){ 6 moves += Math.abs(num-median); 7 } 8 return moves; 9 } 10 11 private int findK(int [] nums, int k, int start, int end){ 12 if(start >= end){ 13 return nums[start]; 14 } 15 int m = partition(nums, start, end); 16 if(m == k){ 17 return nums[m]; 18 }else if(m < k){ 19 return findK(nums, k, m+1, end); 20 }else{ 21 return findK(nums, k, start, m-1); 22 } 23 } 24 25 private int partition(int [] nums, int start, int end){ 26 int pivot = nums[start]; 27 int m = start; 28 int n = start + 1; 29 while(n <= end){ 30 if(nums[n] < pivot){ 31 swap(nums, ++m, n); 32 } 33 n++; 34 } 35 swap(nums, start, m); 36 return m; 37 } 38 39 private void swap(int [] nums, int i, int j){ 40 int temp = nums[i]; 41 nums[i] = nums[j]; 42 nums[j] = temp; 43 } 44 }
类似Minimum Moves to Equal Array Elements.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6258762.html
LeetCode Minimum Moves to Equal Array Elements II相关推荐
- leetcode 462. Minimum Moves to Equal Array Elements II
这道题目和leetcode453是有联系的,虽然这道题难度为中等,但是我感觉初等难度的453绕的弯子更大一些. 题目:Given a non-empty integer array, find the ...
- LeetCode 462 Minimum Moves to Equal Array Elements II
问题:给出一个数组,每次可以从中选择一个数+1或者-1,使得数组中的所有元素相等,要求操作次数最少. 思路:假设元素分别为x1,x2,...,xn,元素最终等于x,则要求|x1-x|+|x2-x|+. ...
- leetcode 453,462. Minimum Moves to Equal Array Elements I, II | 453, 462. 最少移动次数使数组元素相等(图解)
453. Minimum Moves to Equal Array Elements https://leetcode.com/problems/minimum-moves-to-equal-arra ...
- LeetCode 453. 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 ...
- C#LeetCode刷题之#453-最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3877 访问. 给定一个长度为 n 的非空整数数组,找到让数组所有 ...
- java求最小步数,使数组值相等的最小步数 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 ...
- 453. Minimum Moves to Equal Array Elements (python)
- leetcode-453-Minimum Moves to Equal Array Elements
题目描述: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
最新文章
- AI芯片的过去、现在与未来
- python查看安装包的版本scipy,numpy,matplotlib等
- 无法找到脚本文件adsutil.vbs的解决方法
- 批量修改root密码#shell脚本
- spark mllib源码分析之DecisionTree与GBDT
- Thread.interrupt()方法理解
- 理解OAuth 2.0(转)
- 通杀IIS7.0畸形解析0day漏洞
- 高可用 kubernetes 集群部署实践
- 信息学奥赛一本通(1106:年龄与疾病)
- 利用程序动态管理Web.config文件
- 周记【距gdoi:105天】
- python语言实现医院管理系统
- 无需密码自己卸载深信服EDR软件
- 用html画动漫人物,画动漫人物的步骤?
- STM32F103C8T6下载电路设计
- storm流程——storm
- 神兵利器 nth_element
- 合肥工业大学 慕课 梦溪笔谈 习题答案
- JTW93501单键触摸调光芯片详细介绍