Minimum Increment to Make Array Unique LT945
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:
0 <= A.length <= 40000
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的更多相关文章
- [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 ...
- 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 ...
- 【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. ...
- 【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解,TLE 一次遍历 日期 题目地址:http ...
- 【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 ...
- [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 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- 【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 ...
随机推荐
- JavaScript常见的代码精简
1.&& callback && callback() 等价于: if(callback){ callback(); } 表达的意思: 先判断 callback 是不是 ...
- 【剑指offer】判断出栈序列是否合法
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应 ...
- jQuery解决IE6/7/8不能使用 JSON.stringify 函数的问题
原文地址:http://www.ynpxrz.com/n1445665c2023.aspx JSON 对象是在 ECMAScript 第 5 版中实现的,此版于 2009 年 12 月发布:IE6 I ...
- keil5 MDK 链接报错 Error: L6410W 解决
keil5 MDK 报错 Build target 'Project' linking... .\Output\Project.axf: Warning: L6310W: Unable to find ...
- JVM学习总结(一):Java内存区域
一.JVM运行时数据区 1.程序计数器: (1)一块较小的线程私有的内存空间. (2)JVM的多线程是通过线程轮流切换并分配处理器执行时间的方式来实现的,在任何一个确定的时刻,一个处理器(或一个内核) ...
- cookies的常见方式
cookie有如下特点 保存在客户端,一般由浏览器负责存储在本地. 通常是加密存储的,不过由于存储在本地,很难保证数据不被非法访问,并不怎么安全,所以cookies中不宜保存敏感信息,如密码等. 哪些 ...
- uva-10400-搜索
题意:题意很简单,就是输入数字,对数字进行加减乘除,然后能不能得到最后的数字. wa了很多次,memcpy(dst,src,sizeof(dst))才对,最后一个参数写错,最后一个参数是要拷贝的字节数 ...
- linux 查看任务运行时间
ps -eo pid,tty,user,comm,lstart,etime | grep pid 15590 ? meicai java Wed Sep 26 20:04:31 2018 35-15: ...
- win7、centos7 双系统安装总结
centos7安装过程 问题:TroubleShooting选项进入图形化界面安装才成功. win7恢复引导区 问题:安装完Centos后,win7的引导区不见了 具体恢复过程:http://www. ...
- Shell脚本21-40例
21.统计数字并求和 计算文档a.txt中每一行中出现的数字个数并且要计算一下整个文档中一共出现了几个数字.例如a.txt内容如下:12aa*lkjskdjalskdflkskdjflkjj 我们脚本 ...