You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md
+33-37
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,15 @@ It can be shown with 5 or less moves that it is impossible for the array to have
57
57
58
58
<!-- solution:start -->
59
59
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}$.
61
69
62
70
<!-- tabs:start -->
63
71
@@ -67,12 +75,10 @@ It can be shown with 5 or less moves that it is impossible for the array to have
function minIncrementForUnique(nums:number[]):number {
138
136
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 ofnums) {
139
+
y=Math.max(y+1, x);
140
+
ans+=y-x;
145
141
}
146
142
returnans;
147
143
}
@@ -155,13 +151,13 @@ function minIncrementForUnique(nums: number[]): number {
155
151
156
152
### Solution 2: Counting + Greedy
157
153
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.
159
155
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.
161
157
162
-
After the iteration, we return the result.
158
+
After completing the iteration, we return the result.
163
159
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.
0 commit comments