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 <= 400000 <= 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 ...
随机推荐
- Ubuntu18.04创建新的系统用户
目标: 1.为测试学习Docker,在虚拟机OS为18.04里,创建一个系统账号,账号名称:docker 2.在/home下有新建username的文件夹 一.建立账号 1.以root账号登录 2.u ...
- 权限管理RBAC
四张表: 1.module:id/name //模块 2.action:id /module_id/name //权限 3.user:id/name //用户表 4.group:id/user_id ...
- 5.python之pip安装模块失败
本文是篇水文,主要是在学习python过程中总是遇到使用pip安装一些模块失败,记录一下安装模块解决办法 第一种方法: 首先安装wheel模块: pip install wheel 如果wheel都安 ...
- Alternative to iPhone device ID (UDID)
Alternative to iPhone device ID (UDID) [duplicate] up vote10down votefavorite 3 Possible Duplicate:U ...
- 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作
建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...
- 设计模式02: Abstract Factory 抽象工厂(创建型模式)
Abstract Factory 抽象工厂(创建型模式) 常见的对象创建方法: //创建一个Road对象 Road road=new Road(); new的问题: -实现依赖 ...
- C# 取得内网IP、外网IP、客户端IP方法
前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...
- Java集合类总结 (二)
LinkedList类 由于基于数组的链表有一个大的缺点,那就是从链表中间移除一个元素时需要将此元素后面的所有元素向前移动,会产生大量的开销,同样的在链表中间插入一个新元素也会有大量开销.如下图: L ...
- 浅谈短视频APP的发展趋势
2014年6月20日,在AppAnnie最新发布5月应用指数中,美拍荣登“非游戏类iOS榜单”全球下载量第一位置,成为全球iOS应用商店最热门APP.能在<AppAnnie应用指数>这份A ...
- WinForm 中使用 Action 子线程对主线程 控制进行访问
/// <summary> /// 开启新线程执行 /// </summary> /// <param name="sender"></p ...