Skip to content

Commit 02c8054

Browse files
authored
feat: add weekly contest 460 (#4600)
1 parent 1838ba4 commit 02c8054

File tree

18 files changed

+1152
-1
lines changed

18 files changed

+1152
-1
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3627.Maximum%20Median%20Sum%20of%20Subsequences%20of%20Size%203/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3627. 中位数之和的最大值](https://leetcode.cn/problems/maximum-median-sum-of-subsequences-of-size-3)
10+
11+
[English Version](/solution/3600-3699/3627.Maximum%20Median%20Sum%20of%20Subsequences%20of%20Size%203/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code>,其长度可以被 3 整除。</p>
18+
19+
<p>你需要通过多次操作将数组清空。在每一步操作中,你可以从数组中选择任意三个元素,计算它们的&nbsp;<strong>中位数&nbsp;</strong>,并将这三个元素从数组中移除。</p>
20+
21+
<p>奇数长度数组的&nbsp;<strong>中位数&nbsp;</strong>定义为数组按非递减顺序排序后位于中间的元素。</p>
22+
23+
<p>返回通过所有操作得到的&nbsp;<strong>中位数之和的最大值&nbsp;</strong>。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">nums = [2,1,3,2,1,3]</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">5</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<ul>
37+
<li>第一步,选择下标为 2、4 和 5 的元素,它们的中位数是 3。移除这些元素后,<code>nums</code> 变为 <code>[2, 1, 2]</code>。</li>
38+
<li>第二步,选择下标为 0、1 和 2 的元素,它们的中位数是 2。移除这些元素后,<code>nums</code> 变为空数组。</li>
39+
</ul>
40+
41+
<p>因此,中位数之和为 <code>3 + 2 = 5</code>。</p>
42+
</div>
43+
44+
<p><strong class="example">示例 2:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>输入:</strong> <span class="example-io">nums = [1,1,10,10,10,10]</span></p>
48+
49+
<p><strong>输出:</strong> <span class="example-io">20</span></p>
50+
51+
<p><strong>解释:</strong></p>
52+
53+
<ul>
54+
<li>第一步,选择下标为 0、2 和 3 的元素,它们的中位数是 10。移除这些元素后,<code>nums</code> 变为 <code>[1, 10, 10]</code>。</li>
55+
<li>第二步,选择下标为 0、1 和 2 的元素,它们的中位数是 10。移除这些元素后,<code>nums</code> 变为空数组。</li>
56+
</ul>
57+
58+
<p>因此,中位数之和为 <code>10 + 10 = 20</code>。</p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li>
67+
<li><code>nums.length % 3 == 0</code></li>
68+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
69+
</ul>
70+
71+
<!-- description:end -->
72+
73+
## 解法
74+
75+
<!-- solution:start -->
76+
77+
### 方法一:贪心 + 排序
78+
79+
为了使得中位数之和最大,我们需要尽可能选择较大的元素作为中位数。由于每次操作只能选择三个元素,因此我们可以将数组排序后,从下标 $n / 3$ 元素开始,每两个元素选择一个小的,直到数组末尾。这样可以确保我们选择的中位数是最大的。
80+
81+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
82+
83+
<!-- tabs:start -->
84+
85+
#### Python3
86+
87+
```python
88+
class Solution:
89+
def maximumMedianSum(self, nums: List[int]) -> int:
90+
nums.sort()
91+
return sum(nums[len(nums) // 3 :: 2])
92+
```
93+
94+
#### Java
95+
96+
```java
97+
class Solution {
98+
public long maximumMedianSum(int[] nums) {
99+
Arrays.sort(nums);
100+
int n = nums.length;
101+
long ans = 0;
102+
for (int i = n / 3; i < n; i += 2) {
103+
ans += nums[i];
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
long long maximumMedianSum(vector<int>& nums) {
116+
ranges::sort(nums);
117+
int n = nums.size();
118+
long long ans = 0;
119+
for (int i = n / 3; i < n; i += 2) {
120+
ans += nums[i];
121+
}
122+
return ans;
123+
}
124+
};
125+
```
126+
127+
#### Go
128+
129+
```go
130+
func maximumMedianSum(nums []int) (ans int64) {
131+
sort.Ints(nums)
132+
n := len(nums)
133+
for i := n / 3; i < n; i += 2 {
134+
ans += int64(nums[i])
135+
}
136+
return
137+
}
138+
```
139+
140+
#### TypeScript
141+
142+
```ts
143+
function maximumMedianSum(nums: number[]): number {
144+
nums.sort((a, b) => a - b);
145+
const n = nums.length;
146+
let ans = 0;
147+
for (let i = n / 3; i < n; i += 2) {
148+
ans += nums[i];
149+
}
150+
return ans;
151+
}
152+
```
153+
154+
<!-- tabs:end -->
155+
156+
<!-- solution:end -->
157+
158+
<!-- problem:end -->
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3627.Maximum%20Median%20Sum%20of%20Subsequences%20of%20Size%203/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3627. Maximum Median Sum of Subsequences of Size 3](https://leetcode.com/problems/maximum-median-sum-of-subsequences-of-size-3)
10+
11+
[中文文档](/solution/3600-3699/3627.Maximum%20Median%20Sum%20of%20Subsequences%20of%20Size%203/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code> with a length divisible by 3.</p>
18+
19+
<p>You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their <strong>median</strong>, and remove the selected elements from the array.</p>
20+
21+
<p>The <strong>median</strong> of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.</p>
22+
23+
<p>Return the <strong>maximum</strong> possible sum of the medians computed from the selected elements.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,2,1,3]</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<ul>
36+
<li>In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, <code>nums</code> becomes <code>[2, 1, 2]</code>.</li>
37+
<li>In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, <code>nums</code> becomes empty.</li>
38+
</ul>
39+
40+
<p>Hence, the sum of the medians is <code>3 + 2 = 5</code>.</p>
41+
</div>
42+
43+
<p><strong class="example">Example 2:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,10,10,10,10]</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">20</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
52+
<ul>
53+
<li>In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, <code>nums</code> becomes <code>[1, 10, 10]</code>.</li>
54+
<li>In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, <code>nums</code> becomes empty.</li>
55+
</ul>
56+
57+
<p>Hence, the sum of the medians is <code>10 + 10 = 20</code>.</p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li>
65+
<li><code>nums.length % 3 == 0</code></li>
66+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
67+
</ul>
68+
69+
<!-- description:end -->
70+
71+
## Solutions
72+
73+
<!-- solution:start -->
74+
75+
### Solution 1: Greedy + Sorting
76+
77+
To maximize the sum of medians, we need to select larger elements as medians whenever possible. Since each operation can only select three elements, we can sort the array and then start from index $n / 3$, selecting every other element (skipping one) until the end of the array. This ensures that we select the largest possible medians.
78+
79+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$.
80+
81+
<!-- tabs:start -->
82+
83+
#### Python3
84+
85+
```python
86+
class Solution:
87+
def maximumMedianSum(self, nums: List[int]) -> int:
88+
nums.sort()
89+
return sum(nums[len(nums) // 3 :: 2])
90+
```
91+
92+
#### Java
93+
94+
```java
95+
class Solution {
96+
public long maximumMedianSum(int[] nums) {
97+
Arrays.sort(nums);
98+
int n = nums.length;
99+
long ans = 0;
100+
for (int i = n / 3; i < n; i += 2) {
101+
ans += nums[i];
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
#### C++
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
long long maximumMedianSum(vector<int>& nums) {
114+
ranges::sort(nums);
115+
int n = nums.size();
116+
long long ans = 0;
117+
for (int i = n / 3; i < n; i += 2) {
118+
ans += nums[i];
119+
}
120+
return ans;
121+
}
122+
};
123+
```
124+
125+
#### Go
126+
127+
```go
128+
func maximumMedianSum(nums []int) (ans int64) {
129+
sort.Ints(nums)
130+
n := len(nums)
131+
for i := n / 3; i < n; i += 2 {
132+
ans += int64(nums[i])
133+
}
134+
return
135+
}
136+
```
137+
138+
#### TypeScript
139+
140+
```ts
141+
function maximumMedianSum(nums: number[]): number {
142+
nums.sort((a, b) => a - b);
143+
const n = nums.length;
144+
let ans = 0;
145+
for (let i = n / 3; i < n; i += 2) {
146+
ans += nums[i];
147+
}
148+
return ans;
149+
}
150+
```
151+
152+
<!-- tabs:end -->
153+
154+
<!-- solution:end -->
155+
156+
<!-- problem:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
long long maximumMedianSum(vector<int>& nums) {
4+
ranges::sort(nums);
5+
int n = nums.size();
6+
long long ans = 0;
7+
for (int i = n / 3; i < n; i += 2) {
8+
ans += nums[i];
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func maximumMedianSum(nums []int) (ans int64) {
2+
sort.Ints(nums)
3+
n := len(nums)
4+
for i := n / 3; i < n; i += 2 {
5+
ans += int64(nums[i])
6+
}
7+
return
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public long maximumMedianSum(int[] nums) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
long ans = 0;
6+
for (int i = n / 3; i < n; i += 2) {
7+
ans += nums[i];
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def maximumMedianSum(self, nums: List[int]) -> int:
3+
nums.sort()
4+
return sum(nums[len(nums) // 3 :: 2])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function maximumMedianSum(nums: number[]): number {
2+
nums.sort((a, b) => a - b);
3+
const n = nums.length;
4+
let ans = 0;
5+
for (let i = n / 3; i < n; i += 2) {
6+
ans += nums[i];
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)