|
1 |
| -# [1380. Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>Given a <code>m * n</code> matrix of <strong>distinct </strong>numbers, return all lucky numbers in the matrix in <strong>any </strong>order.</p> |
5 |
| -
|
6 |
| -<p>A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.</p> |
7 |
| -
|
8 |
| -<p> </p> |
9 |
| -<p><strong>Example 1:</strong></p> |
10 |
| -
|
11 |
| -<pre> |
12 |
| -<strong>Input:</strong> matrix = [[3,7,8],[9,11,13],[15,16,17]] |
13 |
| -<strong>Output:</strong> [15] |
14 |
| -<strong>Explanation:</strong> 15 is the only lucky number since it is the minimum in its row and the maximum in its column |
15 |
| -</pre> |
16 |
| -
|
17 |
| -<p><strong>Example 2:</strong></p> |
18 |
| -
|
19 |
| -<pre> |
20 |
| -<strong>Input:</strong> matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] |
21 |
| -<strong>Output:</strong> [12] |
22 |
| -<strong>Explanation:</strong> 12 is the only lucky number since it is the minimum in its row and the maximum in its column. |
23 |
| -</pre> |
24 |
| -
|
25 |
| -<p><strong>Example 3:</strong></p> |
26 |
| -
|
27 |
| -<pre> |
28 |
| -<strong>Input:</strong> matrix = [[7,8],[1,2]] |
29 |
| -<strong>Output:</strong> [7] |
30 |
| -</pre> |
31 |
| -
|
32 |
| -<p> </p> |
33 |
| -<p><strong>Constraints:</strong></p> |
34 |
| -
|
35 |
| -<ul> |
36 |
| - <li><code>m == mat.length</code></li> |
37 |
| - <li><code>n == mat[i].length</code></li> |
38 |
| - <li><code>1 <= n, m <= 50</code></li> |
39 |
| - <li><code>1 <= matrix[i][j] <= 10^5</code>.</li> |
40 |
| - <li>All elements in the matrix are distinct.</li> |
41 |
| -</ul> |
42 |
| - |
43 |
| - |
44 |
| -## Solutions |
45 |
| - |
46 |
| - |
47 |
| -### Python3 |
48 |
| - |
49 |
| -```python |
50 |
| - |
51 |
| -``` |
52 |
| - |
53 |
| -### Java |
54 |
| - |
55 |
| -```java |
56 |
| - |
57 |
| -``` |
58 |
| - |
59 |
| -### ... |
60 |
| -``` |
61 |
| - |
62 |
| -``` |
| 1 | +# [1380. Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>Given a <code>m * n</code> matrix of <strong>distinct </strong>numbers, return all lucky numbers in the matrix in <strong>any </strong>order.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p>A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.</p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p> </p> |
| 13 | + |
| 14 | +<p><strong>Example 1:</strong></p> |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +<pre> |
| 19 | + |
| 20 | +<strong>Input:</strong> matrix = [[3,7,8],[9,11,13],[15,16,17]] |
| 21 | + |
| 22 | +<strong>Output:</strong> [15] |
| 23 | + |
| 24 | +<strong>Explanation:</strong> 15 is the only lucky number since it is the minimum in its row and the maximum in its column |
| 25 | + |
| 26 | +</pre> |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +<p><strong>Example 2:</strong></p> |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +<pre> |
| 35 | + |
| 36 | +<strong>Input:</strong> matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] |
| 37 | + |
| 38 | +<strong>Output:</strong> [12] |
| 39 | + |
| 40 | +<strong>Explanation:</strong> 12 is the only lucky number since it is the minimum in its row and the maximum in its column. |
| 41 | + |
| 42 | +</pre> |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +<p><strong>Example 3:</strong></p> |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +<pre> |
| 51 | + |
| 52 | +<strong>Input:</strong> matrix = [[7,8],[1,2]] |
| 53 | + |
| 54 | +<strong>Output:</strong> [7] |
| 55 | + |
| 56 | +</pre> |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +<p> </p> |
| 61 | + |
| 62 | +<p><strong>Constraints:</strong></p> |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +<ul> |
| 67 | + |
| 68 | + <li><code>m == mat.length</code></li> |
| 69 | + |
| 70 | + <li><code>n == mat[i].length</code></li> |
| 71 | + |
| 72 | + <li><code>1 <= n, m <= 50</code></li> |
| 73 | + |
| 74 | + <li><code>1 <= matrix[i][j] <= 10^5</code>.</li> |
| 75 | + |
| 76 | + <li>All elements in the matrix are distinct.</li> |
| 77 | + |
| 78 | +</ul> |
| 79 | + |
| 80 | + |
| 81 | +## Solutions |
| 82 | + |
| 83 | + |
| 84 | +### Python3 |
| 85 | + |
| 86 | +```python |
| 87 | +class Solution: |
| 88 | + def luckyNumbers (self, matrix: List[List[int]]) -> List[int]: |
| 89 | + row_min = {min(rows) for rows in matrix} |
| 90 | + col_max = {max(cols) for cols in zip(*matrix)} |
| 91 | + return [e for e in row_min if e in col_max] |
| 92 | +``` |
| 93 | + |
| 94 | +### Java |
| 95 | + |
| 96 | +```java |
| 97 | +class Solution { |
| 98 | + public List<Integer> luckyNumbers (int[][] matrix) { |
| 99 | + int m = matrix.length, n = matrix[0].length; |
| 100 | + Set<Integer> rowMin = new HashSet<>(); |
| 101 | + List<Integer> res = new ArrayList<>(); |
| 102 | + for (int i = 0; i < m; ++i) { |
| 103 | + int min = Integer.MAX_VALUE; |
| 104 | + for (int j = 0; j < n; ++j) { |
| 105 | + min = Math.min(min, matrix[i][j]); |
| 106 | + } |
| 107 | + rowMin.add(min); |
| 108 | + } |
| 109 | + |
| 110 | + for (int j = 0; j < n; ++j) { |
| 111 | + int max = Integer.MIN_VALUE; |
| 112 | + for (int i = 0; i < m; ++i) { |
| 113 | + max = Math.max(max, matrix[i][j]); |
| 114 | + } |
| 115 | + if (rowMin.contains(max)) { |
| 116 | + res.add(max); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + return res; |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### ... |
| 126 | +``` |
| 127 | +
|
| 128 | +``` |
0 commit comments