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. 关于Java流

  2. Unity Shader Graph(一)初次尝试

    软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 安装依赖项 Window -> Package Manager打开包管理 ...

  3. java中将表单转换为PDF

    经过网上搜索大概有三种方式:PDF模板数据填充,html代码转换pdf,借用wkhtmltopdf工具 一 .PDF模板数据填充 1.新建word,在word中做出和表单一样的布局的空表单,然后另存为 ...

  4. java GC是在什么时候,对什么东西,做了什么事情

    面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?” 面试题目:地球人都知道,Java有个东西叫垃圾收集器,它让创建的对象不需要像c/cpp那样delete.free掉,你 ...

  5. Python学习:模块初识、数据类型

    1.模块初识 在Python中,模块分为两种: (1)标准库 标准库无需安装,只要在使用的时候import就可以使用了 (2)第三方库 第三方库必须下载安装之后才能够引入使用 下面介绍两个基本的模块: ...

  6. awk选取制定行数,条件判断等

    awk '{if(NR%5==0){print}}' your_file 取出可以被5整除的数awk '{if(NR<=300){print}}' your_file 取出行数小于300的数据a ...

  7. autolayout后获取frame

    autolayout设置完layout立即用frame拿对应的值可能拿不准,因为autolayout设置完布局后布局引擎并不会马上去更改布局,而是将布局标记为待更新,此时可以用的方法有两种,一是延时0 ...

  8. Python __slots__限制动态添加变量

    Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭.不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变 ...

  9. 机器学习中的算法(2)-支持向量机(SVM)基础

    版权声明:本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gma ...

  10. type的解释

    在jquery-19.1.1源码中,type,检查对象的类型是:Boolean/Number/String/Function/Array/Date/RegExp/Object/Error中的一种,返回 ...