Skip to content

Commit fd16cac

Browse files
committed
feat: add python and java solutions to leetcode problem No.1380
添加 LeetCode 题解:1380. 矩阵中的幸运数
1 parent af240ee commit fd16cac

File tree

4 files changed

+191
-64
lines changed

4 files changed

+191
-64
lines changed

solution/1300-1399/1380.Lucky Numbers in a Matrix/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,49 @@
5050
## 解法
5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
取行最小值与列最大值的交集即可。
5354

5455
### Python3
5556
<!-- 这里可写当前语言的特殊实现逻辑 -->
5657

5758
```python
58-
59+
class Solution:
60+
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
61+
row_min = {min(rows) for rows in matrix}
62+
col_max = {max(cols) for cols in zip(*matrix)}
63+
return [e for e in row_min if e in col_max]
5964
```
6065

6166
### Java
6267
<!-- 这里可写当前语言的特殊实现逻辑 -->
6368

6469
```java
65-
70+
class Solution {
71+
public List<Integer> luckyNumbers (int[][] matrix) {
72+
int m = matrix.length, n = matrix[0].length;
73+
Set<Integer> rowMin = new HashSet<>();
74+
List<Integer> res = new ArrayList<>();
75+
for (int i = 0; i < m; ++i) {
76+
int min = Integer.MAX_VALUE;
77+
for (int j = 0; j < n; ++j) {
78+
min = Math.min(min, matrix[i][j]);
79+
}
80+
rowMin.add(min);
81+
}
82+
83+
for (int j = 0; j < n; ++j) {
84+
int max = Integer.MIN_VALUE;
85+
for (int i = 0; i < m; ++i) {
86+
max = Math.max(max, matrix[i][j]);
87+
}
88+
if (rowMin.contains(max)) {
89+
res.add(max);
90+
}
91+
92+
}
93+
return res;
94+
}
95+
}
6696
```
6797

6898
### ...
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,128 @@
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&nbsp;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>&nbsp;</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>&nbsp;</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 &lt;= n, m &lt;= 50</code></li>
39-
<li><code>1 &lt;=&nbsp;matrix[i][j]&nbsp;&lt;= 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&nbsp;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>&nbsp;</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>&nbsp;</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 &lt;= n, m &lt;= 50</code></li>
73+
74+
<li><code>1 &lt;=&nbsp;matrix[i][j]&nbsp;&lt;= 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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<Integer> luckyNumbers (int[][] matrix) {
3+
int m = matrix.length, n = matrix[0].length;
4+
Set<Integer> rowMin = new HashSet<>();
5+
List<Integer> res = new ArrayList<>();
6+
for (int i = 0; i < m; ++i) {
7+
int min = Integer.MAX_VALUE;
8+
for (int j = 0; j < n; ++j) {
9+
min = Math.min(min, matrix[i][j]);
10+
}
11+
rowMin.add(min);
12+
}
13+
14+
for (int j = 0; j < n; ++j) {
15+
int max = Integer.MIN_VALUE;
16+
for (int i = 0; i < m; ++i) {
17+
max = Math.max(max, matrix[i][j]);
18+
}
19+
if (rowMin.contains(max)) {
20+
res.add(max);
21+
}
22+
23+
}
24+
return res;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
3+
row_min = {min(rows) for rows in matrix}
4+
col_max = {max(cols) for cols in zip(*matrix)}
5+
return [e for e in row_min if e in col_max]

0 commit comments

Comments
 (0)