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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code