Skip to content

Commit c0a2616

Browse files
add 1380
1 parent fdf8bfb commit c0a2616

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1380.java) | |Easy|Array|
1112
|1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1379.java) | |Medium|Tree|
1213
|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1377.java) | |Hard|DFS, BFS|
1314
|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1376.java) | |Medium|DFS|
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1380. Lucky Numbers in a Matrix
8+
*
9+
* Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.
10+
* A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
11+
*
12+
* Example 1:
13+
* Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
14+
* Output: [15]
15+
* Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column
16+
*
17+
* Example 2:
18+
* Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
19+
* Output: [12]
20+
* Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
21+
*
22+
* Example 3:
23+
* Input: matrix = [[7,8],[1,2]]
24+
* Output: [7]
25+
*
26+
* Constraints:
27+
* m == mat.length
28+
* n == mat[i].length
29+
* 1 <= n, m <= 50
30+
* 1 <= matrix[i][j] <= 10^5.
31+
* All elements in the matrix are distinct.
32+
* */
33+
public class _1380 {
34+
public static class Solution1 {
35+
public List<Integer> luckyNumbers(int[][] matrix) {
36+
List<Integer> result = new ArrayList<>();
37+
for (int i = 0; i < matrix.length; i++) {
38+
for (int j = 0; j < matrix[0].length; j++) {
39+
if (luckyInRow(matrix[i][j], matrix[i])) {
40+
if (luckyInColumn(matrix[i][j], matrix, j)) {
41+
result.add(matrix[i][j]);
42+
}
43+
}
44+
}
45+
}
46+
return result;
47+
}
48+
49+
private boolean luckyInColumn(int number, int[][] matrix, int columnIndex) {
50+
for (int i = 0; i < matrix.length; i++) {
51+
if (number < matrix[i][columnIndex]) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
58+
private boolean luckyInRow(int number, int[] row) {
59+
for (int num : row) {
60+
if (number > num) {
61+
return false;
62+
}
63+
}
64+
return true;
65+
}
66+
}
67+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1380;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1380Test {
12+
private static _1380.Solution1 solution1;
13+
private static int[][] matrix;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1380.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
matrix = new int[][]{
23+
{3, 7, 8},
24+
{9, 11, 13},
25+
{15, 16, 17}
26+
};
27+
assertEquals(Arrays.asList(15), solution1.luckyNumbers(matrix));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy