↓Skip to main content
  1. LeetCode/

LeetCode 3572: Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values

·2 mins· ·
LeetCode Medium Greedy Hash-Map Sorting
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-06-07

  • Submissions: Wrong Answer → Accepted

Approach
#

Pick three indices with pairwise different x values to maximize the sum of their y values.

Greedy: each x value can be used at most once, so use its largest y:

  1. For each x value, keep only the largest y.
  2. If there are fewer than three distinct x values, return -1.
  3. Otherwise, add the three largest y values.

My Attempt: taking the wrong end after sorting
#

max_number_of_groups.sort()
return sum(max_number_of_groups[:3])     # ascending sort: [:3] is the three smallest

After an ascending sort, the first three are the smallest. It should be [-3:].

Solution
#

import heapq

class Solution:
    def maxSumDistinctTriplet(self, x: List[int], y: List[int]) -> int:
        best = {}
        for xi, yi in zip(x, y):
            if yi > best.get(xi, 0):
                best[xi] = yi
        if len(best) < 3:
            return -1
        return sum(heapq.nlargest(3, best.values()))

heapq.nlargest(3, ...) is O(m), slightly faster than sorting the list in O(m log m), and harder to get backwards.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Taking [:3] after an ascending sort.
  • Using the same x twice.

Interview Explanation
#

Each x can be chosen only once, so I keep the largest y for each x. With fewer than three distinct x values I return -1; otherwise I add the three largest y values. One pass with a dict plus nlargest for the top three, so time is O(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 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 3508: Implement Router
·3 mins
LeetCode Medium Design Binary-Search Queue Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent