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 5c36abdb0fe03..72e3e3bfde0bd 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,11 +61,13 @@ tags: ### 方法一:排序 + 贪心 -我们首先对数组进行排序,然后从前往后遍历数组,对于每个元素 `nums[i]`,如果它小于等于前一个元素 `nums[i - 1]`,那么我们将它增加到 `nums[i - 1] + 1`,那么操作的次数就是 `nums[i - 1] - nums[i] + 1`,累加到结果中。 +我们首先对数组 $\textit{nums}$ 进行排序,用一个变量 $\textit{y}$ 记录当前的最大值,初始时 $\textit{y} = -1$。 + +然后遍历数组 $\textit{nums}$,对于每个元素 $x$,我们将 $y$ 更新为 $\max(y + 1, x)$,并将操作次数 $y - x$ 累加到结果中。 遍历完成后,返回结果即可。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `nums` 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 @@ -75,12 +77,10 @@ tags: class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() - ans = 0 - for i in range(1, len(nums)): - if nums[i] <= nums[i - 1]: - d = nums[i - 1] - nums[i] + 1 - nums[i] += d - ans += d + ans, y = 0, -1 + for x in nums: + y = max(y + 1, x) + ans += y - x return ans ``` @@ -90,13 +90,10 @@ class Solution: class Solution { public int minIncrementForUnique(int[] nums) { Arrays.sort(nums); - int ans = 0; - for (int i = 1; i < nums.length; ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; } @@ -110,13 +107,10 @@ class Solution { public: int minIncrementForUnique(vector& nums) { sort(nums.begin(), nums.end()); - int ans = 0; - for (int i = 1; i < nums.size(); ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = max(y + 1, x); + ans += y - x; } return ans; } @@ -128,12 +122,10 @@ public: ```go func minIncrementForUnique(nums []int) (ans int) { sort.Ints(nums) - 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 - } + y := -1 + for _, x := range nums { + y = max(y+1, x) + ans += y - x } return } @@ -144,12 +136,10 @@ func minIncrementForUnique(nums []int) (ans int) { ```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; - } + let [ans, y] = [0, -1]; + for (const x of nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; } @@ -163,13 +153,13 @@ function minIncrementForUnique(nums: number[]): number { ### 方法二:计数 + 贪心 -根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 `cnt` 来记录每个元素出现的次数。 +根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 $\textit{cnt}$ 来记录每个元素出现的次数。 然后从 $0$ 到 $m - 1$ 遍历,对于每个元素 $i$,如果它出现的次数 $\text{cnt}[i]$ 大于 $1$,那么我们将 $\text{cnt}[i] - 1$ 个元素增加到 $i + 1$,并将操作次数累加到结果中。 遍历完成后,返回结果即可。 -时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 `nums` 的长度加上数组 `nums` 的最大值。 +时间复杂度 $O(m)$,空间复杂度 $O(m)$。其中 $m$ 是数组 $\textit{nums}$ 的长度加上数组的最大值。 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 1313b92c6ea6b..0c5e0cfd03638 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 @@ -57,7 +57,15 @@ It can be shown with 5 or less moves that it is impossible for the array to have -### Solution 1 +### Solution 1: Sorting + Greedy + +First, we sort the array $\textit{nums}$, and use a variable $\textit{y}$ to record the current maximum value, initially $\textit{y} = -1$. + +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. + +After completing the iteration, we return the result. + +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}$. @@ -67,12 +75,10 @@ It can be shown with 5 or less moves that it is impossible for the array to have class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() - ans = 0 - for i in range(1, len(nums)): - if nums[i] <= nums[i - 1]: - d = nums[i - 1] - nums[i] + 1 - nums[i] += d - ans += d + ans, y = 0, -1 + for x in nums: + y = max(y + 1, x) + ans += y - x return ans ``` @@ -82,13 +88,10 @@ class Solution: class Solution { public int minIncrementForUnique(int[] nums) { Arrays.sort(nums); - int ans = 0; - for (int i = 1; i < nums.length; ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; } @@ -102,13 +105,10 @@ class Solution { public: int minIncrementForUnique(vector& nums) { sort(nums.begin(), nums.end()); - int ans = 0; - for (int i = 1; i < nums.size(); ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = max(y + 1, x); + ans += y - x; } return ans; } @@ -120,12 +120,10 @@ public: ```go func minIncrementForUnique(nums []int) (ans int) { sort.Ints(nums) - 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 - } + y := -1 + for _, x := range nums { + y = max(y+1, x) + ans += y - x } return } @@ -136,12 +134,10 @@ func minIncrementForUnique(nums []int) (ans int) { ```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; - } + let [ans, y] = [0, -1]; + for (const x of nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; } @@ -155,13 +151,13 @@ function minIncrementForUnique(nums: number[]): number { ### 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. +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. -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. +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. -After the iteration, we return the result. +After completing 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`. +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. diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp index 141f9e629b895..268c0354f83e6 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp @@ -2,13 +2,10 @@ class Solution { public: int minIncrementForUnique(vector& nums) { sort(nums.begin(), nums.end()); - int ans = 0; - for (int i = 1; i < nums.size(); ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = max(y + 1, x); + ans += y - x; } return ans; } diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.go b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.go index 08c6ed100afb6..473c9050389d0 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.go +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.go @@ -1,12 +1,9 @@ -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 - } + y := -1 + for _, x := range nums { + y = max(y+1, x) + ans += y - x } - return ans + return } \ No newline at end of file diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.java b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.java index 79a7f668c0653..2217b7c4b0981 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.java +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.java @@ -1,13 +1,10 @@ class Solution { public int minIncrementForUnique(int[] nums) { Arrays.sort(nums); - int ans = 0; - for (int i = 1; i < nums.length; ++i) { - if (nums[i] <= nums[i - 1]) { - int d = nums[i - 1] - nums[i] + 1; - nums[i] += d; - ans += d; - } + int ans = 0, y = -1; + for (int x : nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; } diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.py b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.py index 9d439f5992564..0c565cba79c8f 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.py +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.py @@ -1,10 +1,8 @@ class Solution: def minIncrementForUnique(self, nums: List[int]) -> int: nums.sort() - ans = 0 - for i in range(1, len(nums)): - if nums[i] <= nums[i - 1]: - d = nums[i - 1] - nums[i] + 1 - nums[i] += d - ans += d + ans, y = 0, -1 + for x in nums: + y = max(y + 1, x) + ans += y - x 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 index f69153a11a7f2..62cf9a477f59f 100644 --- 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 @@ -1,11 +1,9 @@ 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; - } + let [ans, y] = [0, -1]; + for (const x of nums) { + y = Math.max(y + 1, x); + ans += y - x; } return ans; }