【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 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]
题意:
用最小的操作使数组中所有的值相等。
思路:
排序,找出数组中所有的值和索引中间位的插值的绝对值的和
这个题是用Python写的,这次见识Python的高效了,
class Solution(object):
def minMoves2(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
mid=nums[len(nums)/2]
return sum(abs(mid-num) for num in nums)
【LeetCode】462. Minimum Moves to Equal Array Elements II的更多相关文章
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 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说明:只有两个动作是必 ...
- 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] 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 II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
随机推荐
- Object转换为字符并去空格
<div id="txt" style="display:none">1."不积跬步,无以至千里"的古语说明( A ) A.没有 ...
- 又遇Release编译的一坑 -- 应用程序正常初始化(0xc000007b)失败。请单击“确定”,终止应用程序。
项目中使用了xlslib库,以动态库形式编译,由于它没有生成链接库lib文件,所以官方提供的demo中有createDLL这个小程序用来生成lib文件.然而我又 no zuo no die了一次. ...
- Linux下的数据监控工具
Vmstat Vmstat,virtual memmory statistics(虚拟内存统计),主要是对操作系统的内存信息.进程状态.cpu活动等进行监视,但是它不能对某个进程进行深入的分析. Pr ...
- gc学习(转)
一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 VS. 并行 2.2 并发 VS. stop-the-world 2.3 压缩 VS. 不压缩 VS. 复制 ...
- sharedMesh变量
在unity引擎中, 一般情况不建议用sharedMesh变量去写操作,建议只读,因为那会对mesh永久改变 变
- redis配置文件redis.conf的参数说明
打开redis.conf文件: # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that R ...
- Chapter 2 Open Book——34
His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...
- scale-free network
原文链接:http://lihailian.bokee.com/6013647.html 1.什么是无尺度现象? 统计物理学家习惯于把服从幂次分布的现象称为无尺度现象. 在做大量统计实验之前,科学家预 ...
- live555—VS2010/VS2013 下live555编译、使用及测试(转载)
Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等 的支持.Live555实现了对多种音视频 ...
- 【锋利的Jquery】读书笔记二
一.jquery选择器 基本选择器 层次选择器 过滤选择器 基本过滤 内容过滤 可见性过滤 属性过滤 子元素过滤 first : 获取单个元素 $("div:first&quo ...