题目如下:

Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.

Return the least number of moves to make every value in A unique.

Example 1:

Input: [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].

Example 2:

Input: [3,2,1,2,1,7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values. 

Note:

  1. 0 <= A.length <= 40000
  2. 0 <= A[i] < 40000

解题思路:本题的解法类似贪心算法。首先找出所有重复的值并且按升序排好,对于每个重复的值,都找出最小的并且大于自身以及当前不与其他值重复的值,两者的差值即为move的步数。

代码如下:

class Solution(object):
def minIncrementForUnique(self, A):
"""
:type A: List[int]
:rtype: int
"""
dic = {}
dup = []
for i in A:
if i in dic:
dup.append(i)
else:
dic[i] = 1
dup.sort()
res = 0
current = 0
for i in dup:
current = max(i+1,current)
while True:
if current in dic:
current += 1
continue
dic[current] = 1
res += (current - i)
current += 1
break
return res

【leetcode】945. Minimum Increment to Make Array Unique的更多相关文章

  1. 【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解,TLE 一次遍历 日期 题目地址:http ...

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

  3. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  4. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  5. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  6. 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

    Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...

  7. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  8. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  9. 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)

    1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

随机推荐

  1. python基础知识(1)(个人整理)

    import文件夹下的py文件: 情况1: `-- src    |-- mod1.py    `-- test1.py 直接 import mod1.py即可 情况2: -- src |-- mod ...

  2. Java基本数据类型及所占字节大小

    一.Java基本数据类型 基本数据类型有8种:byte.short.int.long.float.double.boolean.char 分为4类:整数型.浮点型.布尔型.字符型. 整数型:byte. ...

  3. SQL笔试

    一.题目 二.练习数据 SELECT VERSION(); create table CDM_AGENT(ano VARCHAR(6),aname VARCHAR(8)); create table ...

  4. boost tuple

    boost::tuple is a generalized version of std::pair. While std::pair can only store exactly two value ...

  5. 【BZOJ1801】【DTOJ2004】 [Ahoi2009]chess 中国象棋 【DP】

    题解: 首先知道一个性质,每一行每一列都最多有两个炮 那么很显然是DP 设F[i][j][k]表示前i行,有j列有一个炮,有k列有两个炮,那么转移式子为 这一行什么都不做:f[i][j][k]=f[i ...

  6. CodeForces - 990G (点分治+链表计数)

    题目:https://vjudge.net/contest/307753#problem/J 题意:一棵树,每个点都有个权值,现在问你,树上gcd每个不同的数有多少个 思路:点分治,首先范围只有 1e ...

  7. js策略模式vs状态模式

    一.策略模式 1.定义:把一些小的算法,封装起来,使他们之间可以相互替换(把代码的实现和使用分离开来)2.利用策略模式实现小方块缓动 html代码: <div id="containe ...

  8. (转)docker-compose安装

    转:https://blog.csdn.net/pushiqiang/article/details/78682323 https://blog.csdn.net/ericnany/article/d ...

  9. PHP运行出现Notice

    问题: Notice:Notice是PHP 的提示而非报错,PHP本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示.在网站正式开始运行时,会把提示关了的. 关闭PHP提示的方法: 搜索ph ...

  10. nRF51822 蓝牙低功耗和 2.4GHz 专利 SoC

    DESCRIPTION nRF51822 是功能强大.高灵活性的多协议 SoC,非常适用于 Bluetooth® 低功耗和 2.4GHz 超低功耗无线应用. nRF51822 基于配备 256kB f ...