题目如下:

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. Linux中Hard link和Symbol link的区别

    Hard link Hard link不能指向不在同一磁盘的文件 Hard link不能指向目录 Hard link与源文件几乎没有区别.只能通过ls -li看出link关系.另外,删除源文件后,Ha ...

  2. 51nod 1253:Kundu and Tree(组合数学)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...

  3. Halo(十)

    Spring Converter(转换器) @FunctionalInterface public interface Converter<S, T> { //一对一转换 @Nullabl ...

  4. springmvc REST风格的URL

    1:需要配置一个filter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELET ...

  5. 远程仓库(GitHub)的使用

    1.注册登录 在 GitHub 上注册登录 暂时忽略不讲 2.创建新的远程仓库 在GitHub网站上创建新的仓库.不管你是先在本地创建仓库还是先在远程创建仓库,要想把代码提交到远程仓库都是要先手动创建 ...

  6. paper 161:python的Json数据解析

    概念 序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON.XML等.反序列化就是从存储区域(JSON,XML)读取反序列化对象的状态 ...

  7. 3 August

    P1013 进制位 结论:加法必为 \(n-1\) 进制:\({(n-1)}^1\) 位必为数字 1:\(0+0=0\). 模拟.字符串. #include <cstdio> #inclu ...

  8. MySQL复制表结构和内容到另一个表中

    一:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已经支持了)1.复制表结构到新表CREATE TABLE 新表LIKE 旧表 2.复制旧表的数据到新表(假设两个表结构一样) ...

  9. 公司-IT-SanSan:SanSan

    ylbtech-公司-IT-SanSan:SanSan 毫不费力的组织.无缝简单.基于名片的联系人管理 SanSan是一个名片管理应用,为企业提供内部联系人管理和分享服务,此外该公司也是日本最大的.基 ...

  10. 测开之路四十九:用Django实现扑克牌游戏

    用Django实现和之前flask一样的扑克牌游戏 项目结构 html <!DOCTYPE html><html lang="en"><head> ...