Skip to content

feat: add solutions to lc problem: No.1877 #4041

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
Feb 7, 2025
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 @@ -82,9 +82,7 @@ $$

最终答案为 $f[n][k]$。

我们注意到 $f[i][j]$ 只跟 $f[i - 1][j - 1]$ 和 $f[i - 1][j]$ 有关,因此可以使用一维数组优化空间复杂度。

时间复杂度 $O(n \times k)$,空间复杂度 $O(k)$。其中 $n$ 和 $k$ 分别是题目中给定的两个整数。
时间复杂度 $O(n \times k)$,空间复杂度 $O(n \times k)$。其中 $n$ 和 $k$ 分别是题目中给定的两个整数。

<!-- tabs:start -->

Expand Down Expand Up @@ -183,7 +181,11 @@ function rearrangeSticks(n: number, k: number): number {

<!-- solution:start -->

### 方法二
### 方法二:动态规划(空间优化)

我们注意到 $f[i][j]$ 只跟 $f[i - 1][j - 1]$ 和 $f[i - 1][j]$ 有关,因此可以使用一维数组优化空间复杂度。

时间复杂度 $O(n \times k)$,空间复杂度 $O(k)$。其中 $n$ 和 $k$ 分别是题目中给定的两个整数。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ $$

The final answer is $f[n][k]$.

We notice that $f[i][j]$ is only related to $f[i - 1][j - 1]$ and $f[i - 1][j]$, so we can use a one-dimensional array to optimize the space complexity.

The time complexity is $O(n \times k)$, and the space complexity is $O(k)$. Here, $n$ and $k$ are the two integers given in the problem.
The time complexity is $O(n \times k)$, and the space complexity is $O(n \times k)$. Where $n$ and $k$ are the two integers given in the problem.

<!-- tabs:start -->

Expand Down Expand Up @@ -184,7 +182,11 @@ function rearrangeSticks(n: number, k: number): number {

<!-- solution:start -->

### Solution 2
### Solution 2: Dynamic Programming (Space Optimization)

We notice that $f[i][j]$ is only related to $f[i - 1][j - 1]$ and $f[i - 1][j]$, so we can use a one-dimensional array to optimize the space complexity.

The time complexity is $O(n \times k)$, and the space complexity is $O(k)$. Here, $n$ and $k$ are the two integers given in the problem.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tags:

因此,我们可以先对数组进行排序,然后使用两个指针分别指向数组的两端,求出两个指针指向的数的和,更新最大数对和的值,然后将左指针右移一位,右指针左移一位,继续进行操作,直到两个指针相遇为止,即可得到最小的最大数对和。

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

<!-- tabs:start -->

Expand All @@ -87,8 +87,7 @@ tags:
class Solution:
def minPairSum(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
return max(x + nums[n - i - 1] for i, x in enumerate(nums[: n >> 1]))
return max(x + nums[-i - 1] for i, x in enumerate(nums[: len(nums) >> 1]))
```

#### Java
Expand All @@ -112,7 +111,7 @@ class Solution {
class Solution {
public:
int minPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, n = nums.size();
for (int i = 0; i < n >> 1; ++i) {
ans = max(ans, nums[i] + nums[n - i - 1]);
Expand Down Expand Up @@ -149,6 +148,41 @@ function minPairSum(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_pair_sum(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
let mut ans = 0;
let n = nums.len();
for i in 0..n / 2 {
ans = ans.max(nums[i] + nums[n - i - 1]);
}
ans
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function (nums) {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - 1 - i]);
}
return ans;
};
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

To minimize the maximum pair sum in the array, we can pair the smallest number with the largest number, the second smallest with the second largest, and so on.

Therefore, we can first sort the array, then use two pointers to point to the two ends of the array. Calculate the sum of the numbers pointed to by the two pointers, update the maximum pair sum, then move the left pointer one step to the right and the right pointer one step to the left. Continue this process until the two pointers meet, and we will get the minimum maximum pair sum.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand All @@ -103,8 +109,7 @@ The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
class Solution:
def minPairSum(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
return max(x + nums[n - i - 1] for i, x in enumerate(nums[: n >> 1]))
return max(x + nums[-i - 1] for i, x in enumerate(nums[: len(nums) >> 1]))
```

#### Java
Expand All @@ -128,7 +133,7 @@ class Solution {
class Solution {
public:
int minPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, n = nums.size();
for (int i = 0; i < n >> 1; ++i) {
ans = max(ans, nums[i] + nums[n - i - 1]);
Expand Down Expand Up @@ -165,6 +170,41 @@ function minPairSum(nums: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_pair_sum(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
let mut ans = 0;
let n = nums.len();
for i in 0..n / 2 {
ans = ans.max(nums[i] + nums[n - i - 1]);
}
ans
}
}
```

#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function (nums) {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - 1 - i]);
}
return ans;
};
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public:
int minPairSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
int ans = 0, n = nums.size();
for (int i = 0; i < n >> 1; ++i) {
ans = max(ans, nums[i] + nums[n - i - 1]);
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function (nums) {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - 1 - i]);
}
return ans;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Solution:
def minPairSum(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
return max(x + nums[n - i - 1] for i, x in enumerate(nums[: n >> 1]))
return max(x + nums[-i - 1] for i, x in enumerate(nums[: len(nums) >> 1]))
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn min_pair_sum(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
let mut ans = 0;
let n = nums.len();
for i in 0..n / 2 {
ans = ans.max(nums[i] + nums[n - i - 1]);
}
ans
}
}