原题链接在这里: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相关推荐

  1. leetcode 462. Minimum Moves to Equal Array Elements II

    这道题目和leetcode453是有联系的,虽然这道题难度为中等,但是我感觉初等难度的453绕的弯子更大一些. 题目:Given a non-empty integer array, find the ...

  2. LeetCode 462 Minimum Moves to Equal Array Elements II

    问题:给出一个数组,每次可以从中选择一个数+1或者-1,使得数组中的所有元素相等,要求操作次数最少. 思路:假设元素分别为x1,x2,...,xn,元素最终等于x,则要求|x1-x|+|x2-x|+. ...

  3. 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 ...

  4. 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 ...

  5. C#LeetCode刷题之#453-最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3877 访问. 给定一个长度为 n 的非空整数数组,找到让数组所有 ...

  6. 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 ...

  7. 453. Minimum Moves to Equal Array Elements (python)

  8. 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 ...

  9. 【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 ...

最新文章

  1. AI芯片的过去、现在与未来
  2. python查看安装包的版本scipy,numpy,matplotlib等
  3. 无法找到脚本文件adsutil.vbs的解决方法
  4. 批量修改root密码#shell脚本
  5. spark mllib源码分析之DecisionTree与GBDT
  6. Thread.interrupt()方法理解
  7. 理解OAuth 2.0(转)
  8. 通杀IIS7.0畸形解析0day漏洞
  9. 高可用 kubernetes 集群部署实践
  10. 信息学奥赛一本通(1106:年龄与疾病)
  11. 利用程序动态管理Web.config文件
  12. 周记【距gdoi:105天】
  13. python语言实现医院管理系统
  14. 无需密码自己卸载深信服EDR软件
  15. 用html画动漫人物,画动漫人物的步骤?
  16. STM32F103C8T6下载电路设计
  17. storm流程——storm
  18. 神兵利器 nth_element
  19. 合肥工业大学 慕课 梦溪笔谈 习题答案
  20. JTW93501单键触摸调光芯片详细介绍

热门文章

  1. 使用Python开发的POC多线程批量执行小框架
  2. 基础篇:如何做一名专业的软件测试工程师
  3. Android_微信_设置
  4. mac下安装apc并且使用
  5. 2015.07.20MapReducer源码解析(笔记)
  6. Aerospike系列:5:安装AMC
  7. Python sys.path详解
  8. Access 数据库连接字符串 (有密码)
  9. 接口转发和重定向区别(三)
  10. DES/3DES/AES区别