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. opennebula 安装指定参数

    [root@opennebula opennebula-]# ./install.sh -u oneadmin -g oneadmin -k -d /home/oneadmin/ -u 指定用户-g ...

  2. Linux 与 BSD

    1)Linux 与 BSD 有什么不同? http://linux.cn/article-3186-1.html 2)BSD(Unix)家族 http://blog.csdn.net/cradmin/ ...

  3. Luogu 4951 [USACO 2001 OPEN]地震

    水个博客玩. $01$分数规划. 题目要求$\frac{F - \sum_{i = 1}^{n}C_i}{T_i}$最大,设$\frac{F - \sum_{i}C_i}{T_i} \geq e$,移 ...

  4. 使用pycharm运行调试scrapy

    摘要 Scrapy是爬虫抓取框架,Pycharm是强大的python的IDE,为了方便使用需要在PyCharm对scrapy程序进行调试 python PyCharm Scrapy scrapy指令其 ...

  5. Servlet请求转发 RequestDispatcher接口.RP

    在Servlet中,利用RequestDispatcher对象,可以将请求转发给另外一个Servlet或JSP页面,甚至是HTML页面,来处理对请求的响应. 一,RequestDispatcher接口 ...

  6. 黑盒测试实践--Day6 11.30

    黑盒测试实践--Day6 11.30 今天完成任务情况: 应用设计的场景用例,完成测试用例的编写 完成测试用例在自动化测试工具QTP上的测试 分析测试结果得到缺陷报告 小靳 软件测试 今天主要钻研了q ...

  7. MongoDB整理笔记のReplica oplog

    主从操作日志oplog MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog".oplog.rs是一个固定长度的capped coll ...

  8. iOS wkwebview https 加载不受信用的站点

    iOS 9.0以上直接设置WKNavigationDelegate代理 func webView(_ webView: WKWebView, didReceive challenge: URLAuth ...

  9. Java 文件上传至leanCloud

    首先,在Controller端入参设置为 @RequestParam(value = "file",defaultValue = "") MultipartFi ...

  10. 基于verilog的FFT算法8点12位硬件实现

    FFT算法8点12位硬件实现 (verilog) 1 一.功能描述: 1 二.设计结构: 2 三.设计模块介绍 3 1.蝶形运算(第一级) 3 2.矢量角度旋转(W) 4 3.CORDIC 结果处理 ...