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..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,10 +135,147 @@ func minIncrementForUnique(nums []int) int { 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; +} +``` + + + + + + + +### 方法二:计数 + 贪心 + +根据题目描述,结果数组的最大值 $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 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; +} +``` + 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..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,10 +127,147 @@ func minIncrementForUnique(nums []int) int { 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; +} +``` + + + + + + + +### 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`. + + + +#### 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 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; +} +``` + 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; +} 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; + } +}; 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 +} 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; + } +} 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 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..5138b72da8254 --- /dev/null +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution2.ts @@ -0,0 +1,16 @@ +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; +}