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 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
问的是把数组的数字+1,几次后,数组的数字唯一了。
这里有个解法,把重复的拿出来,从小到大排序。从[min,max](max不一定就是代码的那个),哪个位置缺了就填哪个,然后算sum +=(i-ans);
其实优雅的解法应该是第二个
class Solution {
public:
int minIncrementForUnique(vector<int>& A) {
int sum = ;
int ans = ;
int pos = ;
int Min = ;
map<int,int> mp;
stack<int>st;
vector<int>ve;
for(int i=;i<A.size();i++){
Min = min(Min,A[i]);
mp[A[i]]++; if(mp[A[i]]>=){
ve.push_back(A[i]);
}
}
sort(ve.begin(),ve.end());
for(int i=ve.size()-;i>=;i--){
st.push(ve[i]);
}
for(int i=;i<;i++){
if(st.empty()){
break;
}
if(mp[i]==){
ans = st.top();
if(i<ans){
continue;
}
sum +=(i-ans);
st.pop();
mp[i]=;
}
}
return sum;
}
};
class Solution {
public:
int minIncrementForUnique(vector<int>& A) {
sort(A.begin(), A.end());
int lowest = -, total = ; for (int a : A) {
lowest = max(lowest, a);
total += lowest - a;
lowest++;
} return total;
}
};
112th LeetCode Weekly Contest Minimum Increment to Make Array Unique的更多相关文章
- 【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解,TLE 一次遍历 日期 题目地址:http ...
- 【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 Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 108th LeetCode Weekly Contest Minimum Falling Path Sum
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- 112th LeetCode Weekly Contest Validate Stack Sequences
Given two sequences pushed and popped with distinct values, return true if and only if this could ha ...
- [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 ...
- 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 ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
随机推荐
- AntD01 Angular2整合AntD、Angular2整合Material、利用Angular2,AntD,Material联合打造后台管理系统 ???
待更新... 2018-5-21 13:53:52 1 环境说明 2 搭建Angular项目 详情参见 -> 点击前往 辅助技能 -> 点击前往 3 创建共享模块 ng g m share ...
- box-shadow 内阴影
1.盒子阴影样式单词:box-shadow 2.语法 代表边框间距靠左0 靠上0 和1px阴影范围 阴影颜色为黑色(#000),有inset 代表框内阴影 ,不带inset 代表框外阴影. 注意:bo ...
- Tensorflow Mask-RCNN训练识别箱子的模型运行结果(练习)
Tensorflow Mask-RCNN训练识别箱子的模型
- 41、OrthoMCL和mcl软件进行基因家族分析
转载:http://www.realbio.cn/news/124.html https://blog.csdn.net/seallama/article/details/43820763 http: ...
- Python基础-3
目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...
- [GO]变量内存和变量地址
package main import "fmt" func main() { //每个变量都有两层含义,变量的内存和变量的地址 fmt.Printf("a = %d\n ...
- Redis 占用Windows系统盘空间23G
Redis常出现问题总结: 1.当出现修改--maxheap and --heapdir 在启动这两个版本时都会创建一个 RedisQFork.dat文件,我不确定 RedisQFork 文件是否变小 ...
- html manifest 离线配置
HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载 ...
- PHP删除目录
function delDir($directory) { if(file_exists($directory)) { $dir_handle = @opendir($directory); if($ ...
- 20165219 学习基础与C语言基础调查
学习基础与C语言基础调查 你有什么技能比大多数人要好? 因为不知道其他人的具体情况,我只能说,我比较擅长钢琴,素描,国画,这也是小时候掌握的比较好的技能. 针对这个技能的获取有什么成功的经验 小时候学 ...