Skip to content

feat: add ts solution to lc problem: No.0945 #3108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ tags:

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

我们首先对数组进行排序,然后从前往后遍历数组,对于每个元素 `nums[i]`,如果它小于等于前一个元素 `nums[i - 1]`,那么我们将它增加到 `nums[i - 1] + 1`,那么操作的次数就是 `nums[i - 1] - nums[i] + 1`,累加到结果中。

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

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `nums` 的长度。

<!-- tabs:start -->

#### Python3
Expand Down Expand Up @@ -120,20 +126,156 @@ public:
#### Go

```go
func minIncrementForUnique(nums []int) int {
func minIncrementForUnique(nums []int) (ans int) {
sort.Ints(nums)
ans := 0
for i := 1; i < len(nums); i++ {
if nums[i] <= nums[i-1] {
d := nums[i-1] - nums[i] + 1
nums[i] += d
ans += d
}
}
return
}
```

#### TypeScript

```ts
function minIncrementForUnique(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 1; i < nums.length; ++i) {
if (nums[i] <= nums[i - 1]) {
ans += nums[i - 1] - nums[i] + 1;
nums[i] = nums[i - 1] + 1;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

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

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

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

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

时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 `nums` 的长度加上数组 `nums` 的最大值。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minIncrementForUnique(self, nums: List[int]) -> int:
m = max(nums) + len(nums)
cnt = Counter(nums)
ans = 0
for i in range(m - 1):
if (diff := cnt[i] - 1) > 0:
cnt[i + 1] += diff
ans += diff
return ans
```

#### Java

```java
class Solution {
public int minIncrementForUnique(int[] nums) {
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
int[] cnt = new int[m];
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
int m = *max_element(nums.begin(), nums.end()) + nums.size();
int cnt[m];
memset(cnt, 0, sizeof(cnt));
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
};
```

#### Go

```go
func minIncrementForUnique(nums []int) (ans int) {
m := slices.Max(nums) + len(nums)
cnt := make([]int, m)
for _, x := range nums {
cnt[x]++
}
for i := 0; i < m-1; i++ {
if diff := cnt[i] - 1; diff > 0 {
cnt[i+1] += diff
ans += diff
}
}
return ans
}
```

#### TypeScript

```ts
function minIncrementForUnique(nums: number[]): number {
const m = Math.max(...nums) + nums.length;
const cnt: number[] = Array(m).fill(0);
for (const x of nums) {
cnt[x]++;
}
let ans = 0;
for (let i = 0; i < m - 1; ++i) {
const diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,156 @@ public:
#### Go

```go
func minIncrementForUnique(nums []int) int {
func minIncrementForUnique(nums []int) (ans int) {
sort.Ints(nums)
ans := 0
for i := 1; i < len(nums); i++ {
if nums[i] <= nums[i-1] {
d := nums[i-1] - nums[i] + 1
nums[i] += d
ans += d
}
}
return
}
```

#### TypeScript

```ts
function minIncrementForUnique(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 1; i < nums.length; ++i) {
if (nums[i] <= nums[i - 1]) {
ans += nums[i - 1] - nums[i] + 1;
nums[i] = nums[i - 1] + 1;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- source:start -->

### Solution 2: Counting + Greedy

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.

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.

After the iteration, we return the result.

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`.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minIncrementForUnique(self, nums: List[int]) -> int:
m = max(nums) + len(nums)
cnt = Counter(nums)
ans = 0
for i in range(m - 1):
if (diff := cnt[i] - 1) > 0:
cnt[i + 1] += diff
ans += diff
return ans
```

#### Java

```java
class Solution {
public int minIncrementForUnique(int[] nums) {
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
int[] cnt = new int[m];
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
int m = *max_element(nums.begin(), nums.end()) + nums.size();
int cnt[m];
memset(cnt, 0, sizeof(cnt));
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
};
```

#### Go

```go
func minIncrementForUnique(nums []int) (ans int) {
m := slices.Max(nums) + len(nums)
cnt := make([]int, m)
for _, x := range nums {
cnt[x]++
}
for i := 0; i < m-1; i++ {
if diff := cnt[i] - 1; diff > 0 {
cnt[i+1] += diff
ans += diff
}
}
return ans
}
```

#### TypeScript

```ts
function minIncrementForUnique(nums: number[]): number {
const m = Math.max(...nums) + nums.length;
const cnt: number[] = Array(m).fill(0);
for (const x of nums) {
cnt[x]++;
}
let ans = 0;
for (let i = 0; i < m - 1; ++i) {
const diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function minIncrementForUnique(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 1; i < nums.length; ++i) {
if (nums[i] <= nums[i - 1]) {
ans += nums[i - 1] - nums[i] + 1;
nums[i] = nums[i - 1] + 1;
}
}
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
int m = *max_element(nums.begin(), nums.end()) + nums.size();
int cnt[m];
memset(cnt, 0, sizeof(cnt));
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
func minIncrementForUnique(nums []int) (ans int) {
m := slices.Max(nums) + len(nums)
cnt := make([]int, m)
for _, x := range nums {
cnt[x]++
}
for i := 0; i < m-1; i++ {
if diff := cnt[i] - 1; diff > 0 {
cnt[i+1] += diff
ans += diff
}
}
return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int minIncrementForUnique(int[] nums) {
int m = Arrays.stream(nums).max().getAsInt() + nums.length;
int[] cnt = new int[m];
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int i = 0; i < m - 1; ++i) {
int diff = cnt[i] - 1;
if (diff > 0) {
cnt[i + 1] += diff;
ans += diff;
}
}
return ans;
}
}
Loading