Note: This title maps to a specific combinatorial logic problem. You are given an array of points, each with an X and Y coordinate. You must select exactly three points. The constraints are: all three points must have strictly distinct X coordinates. Your goal is to maximize the sum of their Y coordinates.
This is a Sorting and Heaps problem. Interviewers ask it to test your data organization skills. A brute force approach evaluating every triplet will fail. It evaluates whether you recognize that, for any given X coordinate, only the point with the absolute highest Y value matters. Once filtered, you just need to pick the three highest Y values.
This problem relies on a Hash Map Filtering and Sorting / Priority Queue pattern.
X -> Max_Y.Points: [(1, 10), (1, 20), (2, 5), (3, 15), (2, 30)]
Step 1: Filter by max Y for each X using a Hash Map.
{1: 20}.{1: 20, 2: 30}.{1: 20, 2: 30, 3: 15}.Step 2: Extract Y values.
Values: [20, 30, 15].
Step 3: Sort descending and take top 3.
Sorted: [30, 20, 15].
Sum = .
This sum is guaranteed to come from distinct X values because our Hash Map enforced that constraint globally.
Candidates often try to sort the original array by Y descending, and then use three pointers or a while loop to find the first three points with different X values. While this works and is , the Hash Map approach is time complexity and vastly easier to implement correctly without risking messy while loop index bounds.
When an interview question asks to maximize a value subject to a "distinct ID" or "distinct category" constraint, your very first instinct should be a Hash Map grouping. Squashing the data down to Category -> Best Value instantly eliminates the complexity of the constraint, allowing you to focus purely on the optimization goal.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Reduce Array Size to The Half | Medium | Solve | |
| Distant Barcodes | Medium | Solve | |
| Task Scheduler | Medium | Solve | |
| Split Array into Consecutive Subsequences | Medium | Solve | |
| Maximum Subsequence Score | Medium | Solve |