Skip to content

feat: update solutions to lc problem: No.0945 #3293

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 1 commit into from
Jul 20, 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,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}$ 的长度。

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -110,13 +107,10 @@ class Solution {
public:
int minIncrementForUnique(vector<int>& 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;
}
Expand All @@ -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
}
Expand All @@ -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;
}
Expand All @@ -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}$ 的长度加上数组的最大值

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ It can be shown with 5 or less moves that it is impossible for the array to have

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -102,13 +105,10 @@ class Solution {
public:
int minIncrementForUnique(vector<int>& 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;
}
Expand All @@ -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
}
Expand All @@ -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;
}
Expand All @@ -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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ class Solution {
public:
int minIncrementForUnique(vector<int>& 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
Loading