From 1c2708f2cb4ecfc3ccc881adf8cee5d18b9a10b7 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 14 Jun 2024 17:12:10 +0300 Subject: [PATCH 1/9] feat: add ts solution to lc problem: No.0945 --- .../README.md | 35 +++++++++++++++++++ .../README_EN.md | 35 +++++++++++++++++++ .../Solution2.ts | 20 +++++++++++ 3 files changed, 90 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md index aa793736e3d5b..622be0911cdcf 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md @@ -138,4 +138,39 @@ func minIncrementForUnique(nums []int) int { + + +### Solution 2 (PLEASE REPLACE WITH CHINESE) + + + +#### TypeScript + +```ts +function minIncrementForUnique(nums: number[]): number { + const n = Math.max(...nums) + 1 + nums.length; + const cnt: number[] = Array.from({ length: n }, () => 0); + let ans = 0; + + for (const x of nums) { + cnt[x]++; + } + + for (let i = 0; i < n; i++) { + if (cnt[i] <= 1) continue; + + const diff = cnt[i] - 1; + cnt[i + 1] += diff; + ans += diff; + cnt[i] = 1; + } + + return ans; +} +``` + + + + + diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md index 12ec668fa0a4c..174ef9c626166 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md @@ -136,4 +136,39 @@ func minIncrementForUnique(nums []int) int { + + +### Solution 2 + + + +#### TypeScript + +```ts +function minIncrementForUnique(nums: number[]): number { + const n = Math.max(...nums) + 1 + nums.length; + const cnt: number[] = Array.from({ length: n }, () => 0); + let ans = 0; + + for (const x of nums) { + cnt[x]++; + } + + for (let i = 0; i < n; i++) { + if (cnt[i] <= 1) continue; + + const diff = cnt[i] - 1; + cnt[i + 1] += diff; + ans += diff; + cnt[i] = 1; + } + + return ans; +} +``` + + + + + diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts new file mode 100644 index 0000000000000..2ca09317b2d76 --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts @@ -0,0 +1,20 @@ +function minIncrementForUnique(nums: number[]): number { + const n = Math.max(...nums) + 1 + nums.length; + const cnt: number[] = Array.from({ length: n }, () => 0); + let ans = 0; + + for (const x of nums) { + cnt[x]++; + } + + for (let i = 0; i < n; i++) { + if (cnt[i] <= 1) continue; + + const diff = cnt[i] - 1; + cnt[i + 1] += diff; + ans += diff; + cnt[i] = 1; + } + + return ans; +} From 4e1ffd04682d35e8d97e97da890d5ccd7b9bf845 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:15:17 +0800 Subject: [PATCH 2/9] Update README.md --- .../README.md | 139 ++++++++++++++++-- 1 file changed, 123 insertions(+), 16 deletions(-) diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md index 622be0911cdcf..5c36abdb0fe03 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md @@ -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` 的长度。 + #### Python3 @@ -120,9 +126,8 @@ 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 @@ -130,7 +135,23 @@ func minIncrementForUnique(nums []int) int { ans += d } } - return ans + 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; } ``` @@ -140,31 +161,117 @@ func minIncrementForUnique(nums []int) int { -### Solution 2 (PLEASE REPLACE WITH CHINESE) +### 方法二:计数 + 贪心 + +根据题目描述,结果数组的最大值 $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` 的最大值。 +#### 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& 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 n = Math.max(...nums) + 1 + nums.length; - const cnt: number[] = Array.from({ length: n }, () => 0); - let ans = 0; - + const m = Math.max(...nums) + nums.length; + const cnt: number[] = Array(m).fill(0); for (const x of nums) { cnt[x]++; } - - for (let i = 0; i < n; i++) { - if (cnt[i] <= 1) continue; - + let ans = 0; + for (let i = 0; i < m - 1; ++i) { const diff = cnt[i] - 1; - cnt[i + 1] += diff; - ans += diff; - cnt[i] = 1; + if (diff > 0) { + cnt[i + 1] += diff; + ans += diff; + } } - return ans; } ``` From 04e126d16406af9d8c60d7735a983a0ac2391c75 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:15:41 +0800 Subject: [PATCH 3/9] Update README_EN.md --- .../README_EN.md | 135 +++++++++++++++--- 1 file changed, 118 insertions(+), 17 deletions(-) diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md index 174ef9c626166..1313b92c6ea6b 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md @@ -118,9 +118,8 @@ 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 @@ -128,7 +127,23 @@ func minIncrementForUnique(nums []int) int { ans += d } } - return ans + 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; } ``` @@ -136,33 +151,119 @@ func minIncrementForUnique(nums []int) int { - + + +### Solution 2: Counting + Greedy -### Solution 2 +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`. +#### 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& 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 n = Math.max(...nums) + 1 + nums.length; - const cnt: number[] = Array.from({ length: n }, () => 0); - let ans = 0; - + const m = Math.max(...nums) + nums.length; + const cnt: number[] = Array(m).fill(0); for (const x of nums) { cnt[x]++; } - - for (let i = 0; i < n; i++) { - if (cnt[i] <= 1) continue; - + let ans = 0; + for (let i = 0; i < m - 1; ++i) { const diff = cnt[i] - 1; - cnt[i + 1] += diff; - ans += diff; - cnt[i] = 1; + if (diff > 0) { + cnt[i + 1] += diff; + ans += diff; + } } - return ans; } ``` From 9039d0c44e2c32124712f9efe4b13b4a78abc951 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:16:53 +0800 Subject: [PATCH 4/9] Create Solution.ts --- .../Solution.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.ts diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.ts b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.ts new file mode 100644 index 0000000000000..f69153a11a7f2 --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.ts @@ -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; +} From f365f7831a0418f58299c7334b410b4cb2bc2bb2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:17:13 +0800 Subject: [PATCH 5/9] Update Solution2.ts --- .../Solution2.ts | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts index 2ca09317b2d76..5138b72da8254 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts @@ -1,20 +1,16 @@ function minIncrementForUnique(nums: number[]): number { - const n = Math.max(...nums) + 1 + nums.length; - const cnt: number[] = Array.from({ length: n }, () => 0); - let ans = 0; - + const m = Math.max(...nums) + nums.length; + const cnt: number[] = Array(m).fill(0); for (const x of nums) { cnt[x]++; } - - for (let i = 0; i < n; i++) { - if (cnt[i] <= 1) continue; - + let ans = 0; + for (let i = 0; i < m - 1; ++i) { const diff = cnt[i] - 1; - cnt[i + 1] += diff; - ans += diff; - cnt[i] = 1; + if (diff > 0) { + cnt[i + 1] += diff; + ans += diff; + } } - return ans; } From 3de894c9d489b7f381e5847c01b49c8ae2987a9a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:17:47 +0800 Subject: [PATCH 6/9] Create Solution2.py --- .../Solution2.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.py diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.py b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.py new file mode 100644 index 0000000000000..0da329d9ac1b7 --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.py @@ -0,0 +1,10 @@ +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 From 204f832b7568493cfe2f35f3af89276abe932502 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:18:32 +0800 Subject: [PATCH 7/9] Create Solution2.java --- .../Solution2.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.java diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.java b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.java new file mode 100644 index 0000000000000..aa4089e68f507 --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.java @@ -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; + } +} From f2b0d70837b3c467d707d0d2c825062599f5a407 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:18:55 +0800 Subject: [PATCH 8/9] Create Solution2.cpp --- .../Solution2.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.cpp diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.cpp b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.cpp new file mode 100644 index 0000000000000..0fe31b5c4b8bd --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minIncrementForUnique(vector& 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; + } +}; From 9471a600b9b3af69dc51190c259ada5c66692363 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 15 Jun 2024 17:19:16 +0800 Subject: [PATCH 9/9] Create Solution2.go --- .../Solution2.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.go diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.go b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.go new file mode 100644 index 0000000000000..ed9437bf5047d --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.go @@ -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 +}