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

问的是把数组的数字+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的更多相关文章

  1. 【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解,TLE 一次遍历 日期 题目地址:http ...

  2. 【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. ...

  3. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

随机推荐

  1. sqlserver临时表或表变量代替游标

    在很多场合,用临时表或表变量也可以替代游标 临时表用在表没有标识列(int)的情况下. 在表有标识列(int)的情况下可以用表变量,当然也可以用临时表. 利用临时表或表变量的原因时,生成一个连续的列 ...

  2. linux下方便的录屏命令

    linux下方便的录屏命令   ffmpeg -f x11grab -s 1024*768 -r 20 -i :0.0 -sameq ~/recode.mpg -r后是刷新屏率,   推出直接Ctrl ...

  3. Sublime Text notes

    1. 设置在窗口右下方显示文件的编码,在user preferences里加上以下的配置 2.设置用新标签页打开新文件而不是用新窗口打开,将以下配置改为false(默认为true)

  4. 算法Sedgewick第四版-第3章Searching-搜索总结

  5. Oracle——SQL基础

    一.SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定义语言DCL: Data ...

  6. excel中COUNTIF的使用

    =(COUNTIF(D9:AH9,"●")+COUNTIF(D7:AH7,"●"))*0.5

  7. 命令之 ulimit

    help ulimit help ulimit ulimit: ulimit [-SHacdefilmnpqrstuvx] [limit] Modify shell resource limits. ...

  8. Java之集合框架vector类设计原理

  9. MySQL性能调优与架构设计——第3章 MySQL存储引擎简介

    第3章 MySQL存储引擎简介 3.1 MySQL 存储引擎概述 MyISAM存储引擎是MySQL默认的存储引擎,也是目前MySQL使用最为广泛的存储引擎之一.他的前身就是我们在MySQL发展历程中所 ...

  10. Jquery delegate的理解

    $(".step4TagList .albumShow", "#divStep4").delegate(" .abmFct .icoRt", ...