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. <ROS> 机器人描述--URDF和XACRO

    文章转自 https://blog.csdn.net/sunbibei/article/details/52297524 特此鸣谢原创作者的辛勤付出 1 URDF 文件 1.1 link和joint ...

  2. STM32定时器时间的计算方法

    本文出自:https://wenku.baidu.com/view/e3bdfb7601f69e31433294c4.htmlSTM32定时器时间的计算方法STM32中的定时器有很多用法:(一)系统时 ...

  3. mysql中的锁

    MYSQL不同的存储引擎支持不同的锁的机制 MyISAM 支持表锁,InnoDB支持表锁和行锁 表锁,行锁比较 表锁:开销小,加锁快:不会出现死锁:锁定力度大,发生锁冲突概率高,并发度最低 行锁:开销 ...

  4. [蓝桥杯]ALGO-181.算法训练_According to Bartjens

    问题描述 计算器和计算机的大量普及也有其弊端.即便是受过专业技术训练的学生们也很可能缺乏计算能力.由于电脑的大量使用,很多人无法心算出7*8这样的算式,甚至是用纸和笔也算不出13*.不过谁在意呢? B ...

  5. springMVC框架返回JSON到前端日期格式化

    在Controller类中,需要返回JSON的方法上加上注释@ResponseBody,这是必须的. 然后spring-servlet.xml配置如下: <?xml version=" ...

  6. openStack instance error 恢复

    cli command下加载openstack超级管理员权限 重设openStack 虚拟机error实例状态即可 nova reset-state instance-id --active

  7. Spring MVC @SessionAttributes注解

    @SessionAttributes原理 默认情况下Spring MVC将模型中的数据存储到request域中.当一个请求结束后,数据就失效了.如果要跨页面使用.那么需要使用到session.而@Se ...

  8. Javascript中 toFixed

    javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...

  9. prim及其练习

    关于prim,其实我今天才学... prim其实就是最小生成树的一种算法,严格每次的找最小边连到树上.看书上的代码看不懂,于是就自己大胆用堆优化写prim. 搞了很长时间,经过不写努力,还是搞出来了. ...

  10. Pandas基础知识(二)

    Pandas的索引对象 index的对象是不可以修改的如执行index[1] = 'f',会报错"Index does not support mutable operations" ...