↓Skip to main content
  1. LeetCode/

LeetCode 3627: Maximum Median Sum of Subsequences of Size 3

·2 mins· ·
LeetCode Medium Math Greedy Sorting Game-Theory Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-27

  • Submissions: Time Limit Exceeded → Accepted

Approach
#

Each step picks three numbers and scores their median. The largest of the three can never be the median, so:

  • Every group takes the two largest remaining plus the smallest remaining as filler. The median is the smaller of the two large ones.
  • The smallest n / 3 numbers all serve as filler.

After sorting, the answer is nums[-2] + nums[-4] + ..., every other value from the end, n / 3 of them.

My Attempts
#

Attempt 1 simulated it: sort, then pop() twice and pop(0) once per step. pop(0) shifts the whole list, O(n) each, O(n^2) overall: TLE. Computing the indices directly passed.

Solution
#

class Solution:
    def maximumMedianSum(self, nums: List[int]) -> int:
        nums.sort()
        n = len(nums)
        return sum(nums[n - 2 - 2 * i] for i in range(n // 3))

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • list.pop(0) is O(n).
  • The largest value in a group can only be its maximum, never its median, so each group “wastes” one large value.

Interview Explanation
#

The largest number can never be a median, so each group takes the two largest remaining and the smallest remaining, scoring the second largest. After sorting, the answer takes every other value from the end, n / 3 of them. Time O(n log n).

Related

LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough
LeetCode 164: Maximum Gap
·3 mins
LeetCode Medium Bucket-Sort Radix-Sort Sorting Array
LeetCode 164 note: the maximum gap is at least the average gap, so with buckets no wider than that it lies between consecutive non-empty buckets; keep each bucket’s min and max for O(n)
LeetCode 1353: Maximum Number of Events That Can Be Attended
·2 mins
LeetCode Medium Heap Greedy Sorting
LeetCode 1353 note: each day, attend the event that ends earliest, using a min-heap of end days