Lucky Numbers in a Matrix - Problem

Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

Input & Output

Example 1 — Basic Case with Lucky Number
$ Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
💡 Note: 15 is the minimum in row 2 [15,16,17] and maximum in column 0 [3,9,15], making it a lucky number
Example 2 — No Lucky Numbers
$ Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: []
💡 Note: No element satisfies both conditions: being minimum in its row AND maximum in its column
Example 3 — Single Element Matrix
$ Input: matrix = [[7]]
Output: [7]
💡 Note: Single element is both minimum in its row and maximum in its column by definition

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 ≤ n, m ≤ 50
  • 1 ≤ matrix[i][j] ≤ 105
  • All elements in the matrix are distinct

Visualization

Tap to expand
Lucky Numbers in a MatrixInput Matrix378191113215161712Row MinColMaxCheck Element 15:Row 2: [15,16,17,12] → min = 15 ✓Col 0: [3,9,15] → max = 15 ✓15 is a Lucky Number!Output:[15]
Understanding the Visualization
1
Input Matrix
3x4 matrix with distinct integers
2
Check Conditions
Find elements that are row minimum AND column maximum
3
Lucky Numbers
Return all elements satisfying both conditions
Key Takeaway
🎯 Key Insight: An element can only be lucky if it's simultaneously the smallest in its row and largest in its column
Asked in
Amazon 25 Microsoft 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 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