From d7894da0a4c955bb29d135c6875d6aa5c524fc72 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 7 Feb 2025 17:00:09 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1877 No.1877.Minimize Maximum Pair Sum in Array --- .../README.md | 10 ++-- .../README_EN.md | 10 ++-- .../README.md | 42 ++++++++++++++-- .../README_EN.md | 48 +++++++++++++++++-- .../Solution.cpp | 4 +- .../Solution.js | 13 +++++ .../Solution.py | 3 +- .../Solution.rs | 12 +++++ 8 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.js create mode 100644 solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.rs diff --git a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md index d7e1502eda2c8..17b8ec70fb732 100644 --- a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md +++ b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md @@ -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$ 分别是题目中给定的两个整数。 @@ -183,7 +181,11 @@ function rearrangeSticks(n: number, k: number): number { -### 方法二 +### 方法二:动态规划(空间优化) + +我们注意到 $f[i][j]$ 只跟 $f[i - 1][j - 1]$ 和 $f[i - 1][j]$ 有关,因此可以使用一维数组优化空间复杂度。 + +时间复杂度 $O(n \times k)$,空间复杂度 $O(k)$。其中 $n$ 和 $k$ 分别是题目中给定的两个整数。 diff --git a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md index 25b5d022febd3..a64bd77dce71a 100644 --- a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md +++ b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md @@ -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. @@ -184,7 +182,11 @@ function rearrangeSticks(n: number, k: number): number { -### 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. diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README.md b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README.md index 7161980fcade3..0c9787b2a07cd 100644 --- a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README.md +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README.md @@ -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}$ 的长度。 @@ -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 @@ -112,7 +111,7 @@ class Solution { class Solution { public: int minPairSum(vector& 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]); @@ -149,6 +148,41 @@ function minPairSum(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_pair_sum(nums: Vec) -> 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 diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md index 27cc1971cb8c9..4e50827246f87 100644 --- a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/README_EN.md @@ -93,7 +93,13 @@ The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8. -### 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}$. @@ -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 @@ -128,7 +133,7 @@ class Solution { class Solution { public: int minPairSum(vector& 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]); @@ -165,6 +170,41 @@ function minPairSum(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_pair_sum(nums: Vec) -> 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 diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.cpp b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.cpp index 217c9a3ee8d5f..62f4c5b952105 100644 --- a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.cpp +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.cpp @@ -1,11 +1,11 @@ class Solution { public: int minPairSum(vector& 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; } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.js b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.js new file mode 100644 index 0000000000000..587d822721587 --- /dev/null +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.js @@ -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; +}; diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.py b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.py index fe4fd92f8dabe..86d2c6cb26288 100644 --- a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.py +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.py @@ -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])) diff --git a/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.rs b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.rs new file mode 100644 index 0000000000000..83f366a47adce --- /dev/null +++ b/solution/1800-1899/1877.Minimize Maximum Pair Sum in Array/Solution.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn min_pair_sum(nums: Vec) -> 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 + } +}