leetcode 462. Minimum Moves to Equal Array Elements II
这道题目和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相关推荐
- 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 Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- 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 Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Woul ...
最新文章
- Node.js开发环境的搭建
- CMD用什么命令可以删除服务?
- 高cpu_再一次生产 CPU 高负载排查实践
- 数据结构:用栈实现表达式的转换(文字描述+详细步骤示例)——中缀转后缀
- 如何展示富文本_自助建站如何做出个性化效果?
- 现在时的条件句_57
- Spring boot--控制器增强
- the python challenge_pythonchallenge 全部关卡提示
- android获取spinner的值_在Android的Spinner中实现键值对的正确方法是什么
- 谷歌怎么找ajax请求,谷歌浏览器 - GET ajax请求失败
- 韦东山嵌入式Linux学习——015 Nand Flash(2)-Nand Flash编程实现读地址信息
- 富文本TinyMCE
- 麦吉尔大学的数学与计算机科学,麦吉尔大学数学与计算机科学本科.pdf
- 每日哲学与编程练习3——无重复数字(Python实现)
- 2548. 大胖子走迷宫 (时间bfs)
- 第十六章 没有银弹 ---软件工程中的根本和次要问题
- Hacked?软件监控邮箱账号是否存在数据泄露情况
- ssRender引擎
- 消防应急照明系统在某数据中心项目的应用
- js下载文件的几种方式