Select Cells in Grid With Maximum Score - Problem

You're given a 2D grid filled with positive integers, and your goal is to become a strategic treasure hunter! 🏆

Your mission: Select cells from the grid to maximize your score (sum of selected values), but there are two crucial rules:

  • No two cells from the same row - each row can contribute at most one treasure
  • All selected values must be unique - no duplicates allowed in your collection

Think of it as choosing the best representatives from each row, where each representative must have a unique value that hasn't been chosen before.

Example: In grid [[1,2,3],[4,3,2],[1,1,1]], you could select 3 from row 0 and 4 from row 1 for a score of 7, but you can't select another 3 since values must be unique!

Input & Output

example_1.py — Basic Grid
$ Input: grid = [[1,2,3],[4,3,2],[1,1,1]]
Output: 8
💡 Note: Select 3 from row 0 (index [0,2]), 4 from row 1 (index [1,0]), and skip row 2 (all values are 1, but 1 hasn't been used yet, so we could pick 1 for a total of 3+4+1=8). Actually, the optimal is to pick 2 from row 0, 4 from row 1, and 1 from row 2 for total 2+4+1=7, or pick 3 from row 0 and 4 from row 1 for 3+4=7. Wait, let me recalculate: pick 3 from row 0, 4 from row 1, 1 from row 2 = 8 total.
example_2.py — Duplicate Values
$ Input: grid = [[7,9,8,6,2],[6,1,1,2,8]]
Output: 17
💡 Note: From row 0, select 9 (highest value). From row 1, select 8 (highest available value that's different from 9). Total: 9 + 8 = 17.
example_3.py — Single Row
$ Input: grid = [[8,7,6]]
Output: 8
💡 Note: With only one row, select the maximum value which is 8.

Constraints

  • 1 ≤ grid.length, grid[i].length ≤ 10
  • 1 ≤ grid[i][j] ≤ 100
  • All values in the grid are positive integers
  • You must select at least one cell to get a non-zero score

Visualization

Tap to expand
Select Cells in Grid With Maximum Score INPUT 2D Grid (3x3) 1 2 3 Row 0 4 3 2 Row 1 1 1 1 Row 2 Input Array: [[1,2,3],[4,3,2],[1,1,1]] Rules: 1. One cell per row max 2. All values must be unique ALGORITHM STEPS 1 Map Values to Rows Group each value with its possible row indices 2 Sort by Value (Desc) Process larger values first for max score 3 DP with Bitmask Track used rows with bit manipulation 4 Greedy Selection Pick 4 from row1, 3 from row0, 1 from row2 Value-Row Mapping 4 --> [row1] 3 --> [row0, row1] 2 --> [row0, row1] 1 --> [row0, row2] FINAL RESULT Selected Cells (Highlighted) 1 2 3 4 3 2 1 1 1 Calculation: 4 + 3 + 1 = 8 All unique, one per row Output: 8 Key Insight: Use bitmask DP to track which rows are used. Process values in descending order and greedily assign each unique value to an available row. This ensures maximum score while satisfying both constraints. TutorialsPoint - Select Cells in Grid With Maximum Score | Optimal Solution
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen