题目如下:

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. 理解"__repr__"

    class aTest: def __repr__(self): return "This is an aTest class." a = aTest() print (a) cl ...

  2. boost serialization

    Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...

  3. PHP curl_multi_getcontent函数

    curl_multi_getcontent — 如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流 说明 string curl_multi_getcontent ( r ...

  4. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  5. 【Linux】ssh执行远程命令awk 参数报错问题

    ssh  ip    sudo docker ps -a | grep none | awk '{print \$1}'| sed 's/%//g' $1前面加上转移符就好

  6. 10.15 sed 命令实践

    在n行前插入 [root@wen data]# sed '2i 106,dandan,CSO' person.txt101,oldboy,CEO106,dandan,CSO102,zhangyao,C ...

  7. Digit

    Digit Accepted : 85   Submit : 308 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 我们把十进制整数依次写成 ...

  8. centos7.4 搭建lnmp

    系统:阿里云 centos7.4 Php:PHP 7.1.13 (cli) Mysql:mysql5.7 Nginx:nginx/1.12.2 一.更新centos7 yum源 cp /etc/yum ...

  9. linux超级块和inode 详解 和 df 、du 命令详解与环境变量

    一.inode块,Unix文件的核心. 首先需要明白的是,在Unix操作系统中的任何资源都被当作文件来管理.如目录.光驱.终端设备等等,都被当作是一种文件.从这方面来说,Unix操作系统中的所有的目录 ...

  10. jenkins插件set build description使用规则

    配置前要注意的点: 先安装插件:set build description 安装该插件后,在[Post-build Actions]栏目中会多出description setter功能,可以实现构建完 ...