Skip to content

Commit dfec18b

Browse files
authored
feat: update solutions to lc problem: No.0945 (#3293)
No.0945.Minimum Increment to Make Array Unique
1 parent 0533267 commit dfec18b

File tree

7 files changed

+81
-108
lines changed

7 files changed

+81
-108
lines changed

Diff for: solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md

+26-36
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ tags:
6161

6262
### 方法一:排序 + 贪心
6363

64-
我们首先对数组进行排序,然后从前往后遍历数组,对于每个元素 `nums[i]`,如果它小于等于前一个元素 `nums[i - 1]`,那么我们将它增加到 `nums[i - 1] + 1`,那么操作的次数就是 `nums[i - 1] - nums[i] + 1`,累加到结果中。
64+
我们首先对数组 $\textit{nums}$ 进行排序,用一个变量 $\textit{y}$ 记录当前的最大值,初始时 $\textit{y} = -1$。
65+
66+
然后遍历数组 $\textit{nums}$,对于每个元素 $x$,我们将 $y$ 更新为 $\max(y + 1, x)$,并将操作次数 $y - x$ 累加到结果中。
6567

6668
遍历完成后,返回结果即可。
6769

68-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `nums` 的长度。
70+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
6971

7072
<!-- tabs:start -->
7173

@@ -75,12 +77,10 @@ tags:
7577
class Solution:
7678
def minIncrementForUnique(self, nums: List[int]) -> int:
7779
nums.sort()
78-
ans = 0
79-
for i in range(1, len(nums)):
80-
if nums[i] <= nums[i - 1]:
81-
d = nums[i - 1] - nums[i] + 1
82-
nums[i] += d
83-
ans += d
80+
ans, y = 0, -1
81+
for x in nums:
82+
y = max(y + 1, x)
83+
ans += y - x
8484
return ans
8585
```
8686

@@ -90,13 +90,10 @@ class Solution:
9090
class Solution {
9191
public int minIncrementForUnique(int[] nums) {
9292
Arrays.sort(nums);
93-
int ans = 0;
94-
for (int i = 1; i < nums.length; ++i) {
95-
if (nums[i] <= nums[i - 1]) {
96-
int d = nums[i - 1] - nums[i] + 1;
97-
nums[i] += d;
98-
ans += d;
99-
}
93+
int ans = 0, y = -1;
94+
for (int x : nums) {
95+
y = Math.max(y + 1, x);
96+
ans += y - x;
10097
}
10198
return ans;
10299
}
@@ -110,13 +107,10 @@ class Solution {
110107
public:
111108
int minIncrementForUnique(vector<int>& nums) {
112109
sort(nums.begin(), nums.end());
113-
int ans = 0;
114-
for (int i = 1; i < nums.size(); ++i) {
115-
if (nums[i] <= nums[i - 1]) {
116-
int d = nums[i - 1] - nums[i] + 1;
117-
nums[i] += d;
118-
ans += d;
119-
}
110+
int ans = 0, y = -1;
111+
for (int x : nums) {
112+
y = max(y + 1, x);
113+
ans += y - x;
120114
}
121115
return ans;
122116
}
@@ -128,12 +122,10 @@ public:
128122
```go
129123
func minIncrementForUnique(nums []int) (ans int) {
130124
sort.Ints(nums)
131-
for i := 1; i < len(nums); i++ {
132-
if nums[i] <= nums[i-1] {
133-
d := nums[i-1] - nums[i] + 1
134-
nums[i] += d
135-
ans += d
136-
}
125+
y := -1
126+
for _, x := range nums {
127+
y = max(y+1, x)
128+
ans += y - x
137129
}
138130
return
139131
}
@@ -144,12 +136,10 @@ func minIncrementForUnique(nums []int) (ans int) {
144136
```ts
145137
function minIncrementForUnique(nums: number[]): number {
146138
nums.sort((a, b) => a - b);
147-
let ans = 0;
148-
for (let i = 1; i < nums.length; ++i) {
149-
if (nums[i] <= nums[i - 1]) {
150-
ans += nums[i - 1] - nums[i] + 1;
151-
nums[i] = nums[i - 1] + 1;
152-
}
139+
let [ans, y] = [0, -1];
140+
for (const x of nums) {
141+
y = Math.max(y + 1, x);
142+
ans += y - x;
153143
}
154144
return ans;
155145
}
@@ -163,13 +153,13 @@ function minIncrementForUnique(nums: number[]): number {
163153

164154
### 方法二:计数 + 贪心
165155

166-
根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 `cnt` 来记录每个元素出现的次数。
156+
根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 $\textit{cnt}$ 来记录每个元素出现的次数。
167157

168158
然后从 $0$ 到 $m - 1$ 遍历,对于每个元素 $i$,如果它出现的次数 $\text{cnt}[i]$ 大于 $1$,那么我们将 $\text{cnt}[i] - 1$ 个元素增加到 $i + 1$,并将操作次数累加到结果中。
169159

170160
遍历完成后,返回结果即可。
171161

172-
时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 `nums` 的长度加上数组 `nums` 的最大值
162+
时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 $\textit{nums}$ 的长度加上数组的最大值
173163

174164
<!-- tabs:start -->
175165

Diff for: solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md

+33-37
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ It can be shown with 5 or less moves that it is impossible for the array to have
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Sorting + Greedy
61+
62+
First, we sort the array $\textit{nums}$, and use a variable $\textit{y}$ to record the current maximum value, initially $\textit{y} = -1$.
63+
64+
Then, we iterate through the array $\textit{nums}$. For each element $x$, we update $y$ to $\max(y + 1, x)$, and accumulate the operation count $y - x$ into the result.
65+
66+
After completing the iteration, we return the result.
67+
68+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
6169

6270
<!-- tabs:start -->
6371

@@ -67,12 +75,10 @@ It can be shown with 5 or less moves that it is impossible for the array to have
6775
class Solution:
6876
def minIncrementForUnique(self, nums: List[int]) -> int:
6977
nums.sort()
70-
ans = 0
71-
for i in range(1, len(nums)):
72-
if nums[i] <= nums[i - 1]:
73-
d = nums[i - 1] - nums[i] + 1
74-
nums[i] += d
75-
ans += d
78+
ans, y = 0, -1
79+
for x in nums:
80+
y = max(y + 1, x)
81+
ans += y - x
7682
return ans
7783
```
7884

@@ -82,13 +88,10 @@ class Solution:
8288
class Solution {
8389
public int minIncrementForUnique(int[] nums) {
8490
Arrays.sort(nums);
85-
int ans = 0;
86-
for (int i = 1; i < nums.length; ++i) {
87-
if (nums[i] <= nums[i - 1]) {
88-
int d = nums[i - 1] - nums[i] + 1;
89-
nums[i] += d;
90-
ans += d;
91-
}
91+
int ans = 0, y = -1;
92+
for (int x : nums) {
93+
y = Math.max(y + 1, x);
94+
ans += y - x;
9295
}
9396
return ans;
9497
}
@@ -102,13 +105,10 @@ class Solution {
102105
public:
103106
int minIncrementForUnique(vector<int>& nums) {
104107
sort(nums.begin(), nums.end());
105-
int ans = 0;
106-
for (int i = 1; i < nums.size(); ++i) {
107-
if (nums[i] <= nums[i - 1]) {
108-
int d = nums[i - 1] - nums[i] + 1;
109-
nums[i] += d;
110-
ans += d;
111-
}
108+
int ans = 0, y = -1;
109+
for (int x : nums) {
110+
y = max(y + 1, x);
111+
ans += y - x;
112112
}
113113
return ans;
114114
}
@@ -120,12 +120,10 @@ public:
120120
```go
121121
func minIncrementForUnique(nums []int) (ans int) {
122122
sort.Ints(nums)
123-
for i := 1; i < len(nums); i++ {
124-
if nums[i] <= nums[i-1] {
125-
d := nums[i-1] - nums[i] + 1
126-
nums[i] += d
127-
ans += d
128-
}
123+
y := -1
124+
for _, x := range nums {
125+
y = max(y+1, x)
126+
ans += y - x
129127
}
130128
return
131129
}
@@ -136,12 +134,10 @@ func minIncrementForUnique(nums []int) (ans int) {
136134
```ts
137135
function minIncrementForUnique(nums: number[]): number {
138136
nums.sort((a, b) => a - b);
139-
let ans = 0;
140-
for (let i = 1; i < nums.length; ++i) {
141-
if (nums[i] <= nums[i - 1]) {
142-
ans += nums[i - 1] - nums[i] + 1;
143-
nums[i] = nums[i - 1] + 1;
144-
}
137+
let [ans, y] = [0, -1];
138+
for (const x of nums) {
139+
y = Math.max(y + 1, x);
140+
ans += y - x;
145141
}
146142
return ans;
147143
}
@@ -155,13 +151,13 @@ function minIncrementForUnique(nums: number[]): number {
155151

156152
### Solution 2: Counting + Greedy
157153

158-
According to the problem description, the maximum value of the result array $m = \max(\text{nums}) + \text{len}(\text{nums})$. We can use a counting array `cnt` to record the occurrence times of each element.
154+
According to the problem description, the maximum value of the result array $m = \max(\text{nums}) + \text{len}(\text{nums})$. We can use a counting array $\textit{cnt}$ to record the occurrence count of each element.
159155

160-
Then, we iterate from $0$ to $m - 1$. For each element $i$, if its occurrence times $\text{cnt}[i]$ is greater than $1$, then we add $\text{cnt}[i] - 1$ elements to $i + 1$ and accumulate the operation times to the result.
156+
Then, we iterate from $0$ to $m - 1$. For each element $i$, if its occurrence count $\textit{cnt}[i]$ is greater than $1$, then we add $\textit{cnt}[i] - 1$ elements to $i + 1$, and accumulate the operation count into the result.
161157

162-
After the iteration, we return the result.
158+
After completing the iteration, we return the result.
163159

164-
The time complexity is $O(m)$, and the space complexity is $O(m)$. Here, $m$ is the length of the array `nums` plus the maximum value in the array `nums`.
160+
The time complexity is $O(m)$, and the space complexity is $O(m)$. Here, $m$ is the length of the array $\textit{nums}$ plus the maximum value in the array.
165161

166162
<!-- tabs:start -->
167163

Diff for: solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ class Solution {
22
public:
33
int minIncrementForUnique(vector<int>& nums) {
44
sort(nums.begin(), nums.end());
5-
int ans = 0;
6-
for (int i = 1; i < nums.size(); ++i) {
7-
if (nums[i] <= nums[i - 1]) {
8-
int d = nums[i - 1] - nums[i] + 1;
9-
nums[i] += d;
10-
ans += d;
11-
}
5+
int ans = 0, y = -1;
6+
for (int x : nums) {
7+
y = max(y + 1, x);
8+
ans += y - x;
129
}
1310
return ans;
1411
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
func minIncrementForUnique(nums []int) int {
1+
func minIncrementForUnique(nums []int) (ans int) {
22
sort.Ints(nums)
3-
ans := 0
4-
for i := 1; i < len(nums); i++ {
5-
if nums[i] <= nums[i-1] {
6-
d := nums[i-1] - nums[i] + 1
7-
nums[i] += d
8-
ans += d
9-
}
3+
y := -1
4+
for _, x := range nums {
5+
y = max(y+1, x)
6+
ans += y - x
107
}
11-
return ans
8+
return
129
}

Diff for: solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution {
22
public int minIncrementForUnique(int[] nums) {
33
Arrays.sort(nums);
4-
int ans = 0;
5-
for (int i = 1; i < nums.length; ++i) {
6-
if (nums[i] <= nums[i - 1]) {
7-
int d = nums[i - 1] - nums[i] + 1;
8-
nums[i] += d;
9-
ans += d;
10-
}
4+
int ans = 0, y = -1;
5+
for (int x : nums) {
6+
y = Math.max(y + 1, x);
7+
ans += y - x;
118
}
129
return ans;
1310
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
def minIncrementForUnique(self, nums: List[int]) -> int:
33
nums.sort()
4-
ans = 0
5-
for i in range(1, len(nums)):
6-
if nums[i] <= nums[i - 1]:
7-
d = nums[i - 1] - nums[i] + 1
8-
nums[i] += d
9-
ans += d
4+
ans, y = 0, -1
5+
for x in nums:
6+
y = max(y + 1, x)
7+
ans += y - x
108
return ans
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
function minIncrementForUnique(nums: number[]): number {
22
nums.sort((a, b) => a - b);
3-
let ans = 0;
4-
for (let i = 1; i < nums.length; ++i) {
5-
if (nums[i] <= nums[i - 1]) {
6-
ans += nums[i - 1] - nums[i] + 1;
7-
nums[i] = nums[i - 1] + 1;
8-
}
3+
let [ans, y] = [0, -1];
4+
for (const x of nums) {
5+
y = Math.max(y + 1, x);
6+
ans += y - x;
97
}
108
return ans;
119
}

0 commit comments

Comments
 (0)