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. VS2010 修改模板文件,增加默认注释

    在开发过程中往往需要在每一个页面(类)增加注释等等内容,VS2010中可以修改模板,在原有模板中增加一个类,会引用System等等命名空 间,以及一些程序集.下面我们来看看如何增加自己需要一些说明,比 ...

  2. [转][C#]文件流读取

    { internal static class FileUtils { public static string GetRelativePath(string absPath, string base ...

  3. Linux和Windows启动后台程序

    平时很多时候,我们需要通过脚本命令调用执行程序,集成一体后方便使用快捷.但是启动脚本窗口比较碍眼,能设置为后台运行既方便又美观. Linux启动后台程序 1.后台执行 nohup方法:不挂断的运行命令 ...

  4. Getting started - RN1

    0. down yarn https://yarnpkg.com 1. Expo Cli 此环境用于开发或学习之用. (1)install npm install -g expo-cli (2) us ...

  5. Error during artifact deployment. See server log for details.

    Error during artifact deployment. See server log for details. 这两个地方要一样.不然.就报 Error during artifact d ...

  6. 优化 SQL SELECT 语句性能

    SELECT语句的性能调优有时是一个非常耗时的任务,在我看来它遵循帕累托原则.20%的努力很可能会给你带来80%的性能提升,而为了获得另外20%的性能提升你可能需要花费80%的时间. 检查索引:在SQ ...

  7. 解决spring-security session超时 Ajax 请求没有重定向的问题

    开始时, 代码是这样的: $.ajax({ type : "POST", url : sSource, cache : false, dataType : "json&q ...

  8. javaweb项目编译错误

    Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误:Description Resource Path Location TypeDynamic Web Module 3.0 r ...

  9. mvc中view与controll之间传递参数时,可以使用url进行传递

    mvc中view与controller之间传递参数时,可以使用url进行传递,但是在url的地址中需要加上“id=123”这样的东西才行. 具体如代码: window.location.href = ...

  10. lambda group by 的用法

    foreach (DynamicObject lstKSItem in lstKSItems) { var entity = lstKSItem["FEntity"] as Dyn ...