这道题目和leetcode453是有联系的,虽然这道题难度为中等,但是我感觉初等难度的453绕的弯子更大一些。

题目: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]

中文版:

给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1。 您可以假设数组的长度最多为10000。

例如:输入:[1,2,3]输出:

2说明:
只有两个动作是必要的(记得每一步仅可使其中一个元素加1或减1): [1,2,3]  =>  [2,2,3]  =>  [2,2,2]

解答思路:首先需要思考将数组中的所有的数都变成一个相同的数,那这个数应该是什么才能使得所有数字移动次数最少?一般脑海中有两种想法:1、中位数  2、平均数。      那就写个小例子看看到底选哪个, 列如 {3,2,1,10} 中位数为2,平均数为4。       如果都移动到中位数需要(3-2)+(2-2)+(2-1)+(10-1)=11次,      如果都移动到中位数需要(4-2)+(4-2)+(4-1)+(10-4)=13次      所以就选中位了。
解答代码:
 1 class Solution {
 2     public int minMoves2(int[] nums) {
 3         Arrays.sort(nums);
 4         int length=nums.length;
 5         int midNum=nums[length/2];
 6         int moveCnt=0;
 7         for (int i = 0; i <length ; i++) {
 8             moveCnt+=Math.abs(nums[i]-midNum);
 9         }
10         return moveCnt;
11     }
12 }

运行结果:

转载于:https://www.cnblogs.com/ming-jia/p/9649926.html

leetcode 462. Minimum Moves to Equal Array Elements II相关推荐

  1. LeetCode 462 Minimum Moves to Equal Array Elements II

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

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

  3. LeetCode Minimum Moves to Equal Array Elements II

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

  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 Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Woul ...

最新文章

  1. Node.js开发环境的搭建
  2. CMD用什么命令可以删除服务?
  3. 高cpu_再一次生产 CPU 高负载排查实践
  4. 数据结构:用栈实现表达式的转换(文字描述+详细步骤示例)——中缀转后缀
  5. 如何展示富文本_自助建站如何做出个性化效果?
  6. 现在时的条件句_57
  7. Spring boot--控制器增强
  8. the python challenge_pythonchallenge 全部关卡提示
  9. android获取spinner的值_在Android的Spinner中实现键值对的正确方法是什么
  10. 谷歌怎么找ajax请求,谷歌浏览器 - GET ajax请求失败
  11. 韦东山嵌入式Linux学习——015 Nand Flash(2)-Nand Flash编程实现读地址信息
  12. 富文本TinyMCE
  13. 麦吉尔大学的数学与计算机科学,麦吉尔大学数学与计算机科学本科.pdf
  14. 每日哲学与编程练习3——无重复数字(Python实现)
  15. 2548. 大胖子走迷宫 (时间bfs)
  16. 第十六章 没有银弹 ---软件工程中的根本和次要问题
  17. Hacked?软件监控邮箱账号是否存在数据泄露情况
  18. ssRender引擎
  19. 消防应急照明系统在某数据中心项目的应用
  20. js下载文件的几种方式

热门文章

  1. 优秀!303篇论文获2020年度“优秀博士学位论文”!
  2. CVPR2020 | 遮挡也能识别?地平线提出用时序信息提升行人检测准确度
  3. 收藏 | 机器学习中常用的5种回归损失函数
  4. 《Python编程从入门到实践》记录之json模块(数据存储)
  5. 《Python编程从入门到实践》记录之读取文件
  6. 《Python编程从入门到实践》记录之列表切片
  7. 推荐系统遇上深度学习(三十九)-推荐系统中召回策略演进!
  8. 深度学习(七十二)ssd物体检测
  9. 水土保持功能评估中k值的计算公式
  10. C中不安全的函数以解决办法汇总