Skip to content

Commit b566241

Browse files
rain84yanglbme
andauthored
feat: add ts solution to lc problem: No.0945 (#3108)
Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 2ad5648 commit b566241

File tree

8 files changed

+371
-4
lines changed

8 files changed

+371
-4
lines changed

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

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ tags:
6161

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

64+
我们首先对数组进行排序,然后从前往后遍历数组,对于每个元素 `nums[i]`,如果它小于等于前一个元素 `nums[i - 1]`,那么我们将它增加到 `nums[i - 1] + 1`,那么操作的次数就是 `nums[i - 1] - nums[i] + 1`,累加到结果中。
65+
66+
遍历完成后,返回结果即可。
67+
68+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `nums` 的长度。
69+
6470
<!-- tabs:start -->
6571

6672
#### Python3
@@ -120,20 +126,156 @@ public:
120126
#### Go
121127
122128
```go
123-
func minIncrementForUnique(nums []int) int {
129+
func minIncrementForUnique(nums []int) (ans int) {
124130
sort.Ints(nums)
125-
ans := 0
126131
for i := 1; i < len(nums); i++ {
127132
if nums[i] <= nums[i-1] {
128133
d := nums[i-1] - nums[i] + 1
129134
nums[i] += d
130135
ans += d
131136
}
132137
}
138+
return
139+
}
140+
```
141+
142+
#### TypeScript
143+
144+
```ts
145+
function minIncrementForUnique(nums: number[]): number {
146+
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+
}
153+
}
154+
return ans;
155+
}
156+
```
157+
158+
<!-- tabs:end -->
159+
160+
<!-- solution:end -->
161+
162+
<!-- solution:start -->
163+
164+
### 方法二:计数 + 贪心
165+
166+
根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 `cnt` 来记录每个元素出现的次数。
167+
168+
然后从 $0$ 到 $m - 1$ 遍历,对于每个元素 $i$,如果它出现的次数 $\text{cnt}[i]$ 大于 $1$,那么我们将 $\text{cnt}[i] - 1$ 个元素增加到 $i + 1$,并将操作次数累加到结果中。
169+
170+
遍历完成后,返回结果即可。
171+
172+
时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 `nums` 的长度加上数组 `nums` 的最大值。
173+
174+
<!-- tabs:start -->
175+
176+
#### Python3
177+
178+
```python
179+
class Solution:
180+
def minIncrementForUnique(self, nums: List[int]) -> int:
181+
m = max(nums) + len(nums)
182+
cnt = Counter(nums)
183+
ans = 0
184+
for i in range(m - 1):
185+
if (diff := cnt[i] - 1) > 0:
186+
cnt[i + 1] += diff
187+
ans += diff
188+
return ans
189+
```
190+
191+
#### Java
192+
193+
```java
194+
class Solution {
195+
public int minIncrementForUnique(int[] nums) {
196+
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
197+
int[] cnt = new int[m];
198+
for (int x : nums) {
199+
++cnt[x];
200+
}
201+
int ans = 0;
202+
for (int i = 0; i < m - 1; ++i) {
203+
int diff = cnt[i] - 1;
204+
if (diff > 0) {
205+
cnt[i + 1] += diff;
206+
ans += diff;
207+
}
208+
}
209+
return ans;
210+
}
211+
}
212+
```
213+
214+
#### C++
215+
216+
```cpp
217+
class Solution {
218+
public:
219+
int minIncrementForUnique(vector<int>& nums) {
220+
int m = *max_element(nums.begin(), nums.end()) + nums.size();
221+
int cnt[m];
222+
memset(cnt, 0, sizeof(cnt));
223+
for (int x : nums) {
224+
++cnt[x];
225+
}
226+
int ans = 0;
227+
for (int i = 0; i < m - 1; ++i) {
228+
int diff = cnt[i] - 1;
229+
if (diff > 0) {
230+
cnt[i + 1] += diff;
231+
ans += diff;
232+
}
233+
}
234+
return ans;
235+
}
236+
};
237+
```
238+
239+
#### Go
240+
241+
```go
242+
func minIncrementForUnique(nums []int) (ans int) {
243+
m := slices.Max(nums) + len(nums)
244+
cnt := make([]int, m)
245+
for _, x := range nums {
246+
cnt[x]++
247+
}
248+
for i := 0; i < m-1; i++ {
249+
if diff := cnt[i] - 1; diff > 0 {
250+
cnt[i+1] += diff
251+
ans += diff
252+
}
253+
}
133254
return ans
134255
}
135256
```
136257

258+
#### TypeScript
259+
260+
```ts
261+
function minIncrementForUnique(nums: number[]): number {
262+
const m = Math.max(...nums) + nums.length;
263+
const cnt: number[] = Array(m).fill(0);
264+
for (const x of nums) {
265+
cnt[x]++;
266+
}
267+
let ans = 0;
268+
for (let i = 0; i < m - 1; ++i) {
269+
const diff = cnt[i] - 1;
270+
if (diff > 0) {
271+
cnt[i + 1] += diff;
272+
ans += diff;
273+
}
274+
}
275+
return ans;
276+
}
277+
```
278+
137279
<!-- tabs:end -->
138280

139281
<!-- solution:end -->

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

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,156 @@ public:
118118
#### Go
119119
120120
```go
121-
func minIncrementForUnique(nums []int) int {
121+
func minIncrementForUnique(nums []int) (ans int) {
122122
sort.Ints(nums)
123-
ans := 0
124123
for i := 1; i < len(nums); i++ {
125124
if nums[i] <= nums[i-1] {
126125
d := nums[i-1] - nums[i] + 1
127126
nums[i] += d
128127
ans += d
129128
}
130129
}
130+
return
131+
}
132+
```
133+
134+
#### TypeScript
135+
136+
```ts
137+
function minIncrementForUnique(nums: number[]): number {
138+
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+
}
145+
}
146+
return ans;
147+
}
148+
```
149+
150+
<!-- tabs:end -->
151+
152+
<!-- solution:end -->
153+
154+
<!-- source:start -->
155+
156+
### Solution 2: Counting + Greedy
157+
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.
159+
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.
161+
162+
After the iteration, we return the result.
163+
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`.
165+
166+
<!-- tabs:start -->
167+
168+
#### Python3
169+
170+
```python
171+
class Solution:
172+
def minIncrementForUnique(self, nums: List[int]) -> int:
173+
m = max(nums) + len(nums)
174+
cnt = Counter(nums)
175+
ans = 0
176+
for i in range(m - 1):
177+
if (diff := cnt[i] - 1) > 0:
178+
cnt[i + 1] += diff
179+
ans += diff
180+
return ans
181+
```
182+
183+
#### Java
184+
185+
```java
186+
class Solution {
187+
public int minIncrementForUnique(int[] nums) {
188+
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
189+
int[] cnt = new int[m];
190+
for (int x : nums) {
191+
++cnt[x];
192+
}
193+
int ans = 0;
194+
for (int i = 0; i < m - 1; ++i) {
195+
int diff = cnt[i] - 1;
196+
if (diff > 0) {
197+
cnt[i + 1] += diff;
198+
ans += diff;
199+
}
200+
}
201+
return ans;
202+
}
203+
}
204+
```
205+
206+
#### C++
207+
208+
```cpp
209+
class Solution {
210+
public:
211+
int minIncrementForUnique(vector<int>& nums) {
212+
int m = *max_element(nums.begin(), nums.end()) + nums.size();
213+
int cnt[m];
214+
memset(cnt, 0, sizeof(cnt));
215+
for (int x : nums) {
216+
++cnt[x];
217+
}
218+
int ans = 0;
219+
for (int i = 0; i < m - 1; ++i) {
220+
int diff = cnt[i] - 1;
221+
if (diff > 0) {
222+
cnt[i + 1] += diff;
223+
ans += diff;
224+
}
225+
}
226+
return ans;
227+
}
228+
};
229+
```
230+
231+
#### Go
232+
233+
```go
234+
func minIncrementForUnique(nums []int) (ans int) {
235+
m := slices.Max(nums) + len(nums)
236+
cnt := make([]int, m)
237+
for _, x := range nums {
238+
cnt[x]++
239+
}
240+
for i := 0; i < m-1; i++ {
241+
if diff := cnt[i] - 1; diff > 0 {
242+
cnt[i+1] += diff
243+
ans += diff
244+
}
245+
}
131246
return ans
132247
}
133248
```
134249

250+
#### TypeScript
251+
252+
```ts
253+
function minIncrementForUnique(nums: number[]): number {
254+
const m = Math.max(...nums) + nums.length;
255+
const cnt: number[] = Array(m).fill(0);
256+
for (const x of nums) {
257+
cnt[x]++;
258+
}
259+
let ans = 0;
260+
for (let i = 0; i < m - 1; ++i) {
261+
const diff = cnt[i] - 1;
262+
if (diff > 0) {
263+
cnt[i + 1] += diff;
264+
ans += diff;
265+
}
266+
}
267+
return ans;
268+
}
269+
```
270+
135271
<!-- tabs:end -->
136272

137273
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minIncrementForUnique(nums: number[]): number {
2+
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+
}
9+
}
10+
return ans;
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minIncrementForUnique(vector<int>& nums) {
4+
int m = *max_element(nums.begin(), nums.end()) + nums.size();
5+
int cnt[m];
6+
memset(cnt, 0, sizeof(cnt));
7+
for (int x : nums) {
8+
++cnt[x];
9+
}
10+
int ans = 0;
11+
for (int i = 0; i < m - 1; ++i) {
12+
int diff = cnt[i] - 1;
13+
if (diff > 0) {
14+
cnt[i + 1] += diff;
15+
ans += diff;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func minIncrementForUnique(nums []int) (ans int) {
2+
m := slices.Max(nums) + len(nums)
3+
cnt := make([]int, m)
4+
for _, x := range nums {
5+
cnt[x]++
6+
}
7+
for i := 0; i < m-1; i++ {
8+
if diff := cnt[i] - 1; diff > 0 {
9+
cnt[i+1] += diff
10+
ans += diff
11+
}
12+
}
13+
return ans
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int minIncrementForUnique(int[] nums) {
3+
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
4+
int[] cnt = new int[m];
5+
for (int x : nums) {
6+
++cnt[x];
7+
}
8+
int ans = 0;
9+
for (int i = 0; i < m - 1; ++i) {
10+
int diff = cnt[i] - 1;
11+
if (diff > 0) {
12+
cnt[i + 1] += diff;
13+
ans += diff;
14+
}
15+
}
16+
return ans;
17+
}
18+
}

0 commit comments

Comments
 (0)