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
 

Idea 1. Once the array is sorted, the next available number is at least a big as the previous + 1,

Time complexity: O(nlogn)

Space complexity: O(1), unless counting the space needed for sorting

 class Solution {
public int minIncrementForUnique(int[] A) {
if(A.length == 0) {
return 0;
} Arrays.sort(A);
int prev = A[0];
int cnt = 0;
for(int i = 1; i < A.length; ++i) {
if(A[i] <= prev) {
cnt += prev - A[i] + 1;
prev += 1;
}
else {
prev = A[i];
}
}
return cnt;
}
}

网上更简洁的方法,亮瞎眼啊

 class Solution {
public int minIncrementForUnique(int[] A) {
if(A.length == 0) {
return 0;
} Arrays.sort(A);
int nextAvailable = A[0] + 1;
int cnt = 0;
for(int i = 1; i < A.length; ++i) {
cnt += Math.max(nextAvailable - A[i], 0);
nextAvailable = Math.max(nextAvailable, A[i]) + 1;
}
return cnt;
}
}

Idea 2. Map + count, didn't get the limit of 10000 at first, then imagin n= 4, instead of 10000, [4, 4, 4, 4], the max would be max(A) + len(A) -1

Time complexity: O(n + max(A)), n = A.length

Space complexity: O(n + max(A))

 class Solution {
public int minIncrementForUnique(int[] A) {
int n = A.length;
int[] count = new int[80000]; for(int a: A) {
++count[a];
} int result = 0;
int needed = 0;
for(int i = 0; i < 80000; ++i) {
if(count[i] >= 2) {
result -= (count[i]-1) * i;
needed += count[i] - 1;
}
else if(needed > 0 && count[i] == 0) {
--needed;
result += i;
}
} return result;
}
}

Minimum Increment to Make Array Unique LT945的更多相关文章

  1. [Swift]LeetCode945. 使数组唯一的最小增量 | Minimum Increment to Make Array Unique

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

  2. 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique

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

  3. 【leetcode】945. Minimum Increment to Make Array Unique

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

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

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

  5. 【leetcode】153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...

  6. [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 ...

  7. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

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

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

随机推荐

  1. 解决双击excel文件打开多个excel.exe进程的问题

    解决双击excel文件打开多个excel.exe进程的问题有些时候,双击两个excel文件,会打开多个excel进程,不同进程之间不能复制粘贴公式,只能粘贴数值,很不方便.怎么样双击多个excel文件 ...

  2. 原生js中用Ajax进行get传参

    原生js中用Ajax进行get传参 案例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  3. 涂抹mysql笔记-mysql字符集

    字符集:查看mysql数据库当前都支持哪些字符集:system@(none)>show character set;+----------+--------------------------- ...

  4. css3属性box-sizing:border-box 用法解析

    响应式Web设计经常需要我们通过百分比设置组件宽度.如果我们不考虑边框,那么很容易就可以实现,但如果你给每一列以及总宽度都采用百分比设置,那这个时候固定的边框大小就会出来捣乱.下面我们将看到一组方法去 ...

  5. 使用睿云智合开源 Breeze 工具部署 Kubernetes v1.12.3 高可用集群

    一.Breeze简介 Breeze 项目是深圳睿云智合所开源的Kubernetes 图形化部署工具,大大简化了Kubernetes 部署的步骤,其最大亮点在于支持全离线环境的部署,且不需要FQ获取 G ...

  6. github 添加wiki

    亲们支持我的新博客哦==>地址(以后更新会尽量在新博客更新,欢迎大家访问加入我的后宫w) ) 平时都是写readMe和docs的,一直眼瞎没有注意到有wiki这个功能 随便找一个写了wiki的看 ...

  7. 学习笔记:jqueryui

    Jquery UI cdn.bootcss.com <!-- jquery --> <script src="http://cdn.bootcss.com/jquery/1 ...

  8. 微信调用itchat库 实现发消息

    import itchat,timefrom itchat.content import * itchat.auto_login(enableCmdQR=-1)while True: for i in ...

  9. 关于页面缩放时css错乱的处理方法---之一

    这几天遇到一个问题,就是在做网页的时候,页面缩放时,布局就乱了,原来的样子不会跟随缩放的放大或者缩小进行改变,直接导致的后果,就是页面很难看,无法使用 之前虽然写了代码,但是一直没有注意到缩放后页面的 ...

  10. Centos7搭建OpenVPN服务器

    Windows下同时连接多个VPN的话,需要以管理员身份运行 C:\Program Files\TAP-Windows\bin\addtap.bat 添加虚拟网络适配器 --------------- ...