forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0945.cpp
More file actions
31 lines (30 loc) · 736 Bytes
/
0945.cpp
File metadata and controls
31 lines (30 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int minIncrementForUnique(vector<int>& A)
{
int box[40000]{};
int result = 0, max_A = 0;
for (auto a : A)
{
box[a]++;
if (max_A < a) max_A = a;
}
for (int i = 0; i < max_A; ++i)
{
if (box[i] <= 1) continue;
int ano = box[i] - 1;
result += ano;
box[i+1] += ano;
box[i] = 1;
}
int last_ano = box[max_A] - 1;
result += (1 + last_ano)*last_ano/2;
return result;
}
};