Skip to content

Commit d9e3da9

Browse files
add 1428
1 parent 3b7ec82 commit d9e3da9

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ _If you like this project, please leave me a star._ ★
1212
|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java) | |Easy|String|
1313
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String|
1414
|1431|[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1431.java) | |Easy|Array|
15+
|1428|[Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1428.java) | |Medium|Array|
1516
|1427|[Perform String Shifts](https://leetcode.com/problems/perform-string-shifts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1427.java) | |Easy|Array, Math|
1617
|1426|[Counting Elements](https://leetcode.com/problems/counting-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1426.java) | |Easy|Array|
1718
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1423.java) | |Medium|Array, DP, Sliding Window|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.BinaryMatrix;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
public class _1428 {
10+
public static class Solution1 {
11+
public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) {
12+
List<Integer> dimensions = binaryMatrix.dimensions();
13+
int m = dimensions.get(0);
14+
int n = dimensions.get(1);
15+
List<int[]> list = new ArrayList();
16+
for (int i = 0; i < m; i++) {
17+
int leftMostColumn = binarySearch(i, binaryMatrix, n - 1);
18+
list.add(new int[]{i, leftMostColumn});
19+
}
20+
Collections.sort(list, (a, b) -> a[1] - b[1]);
21+
return list.get(0)[1] == 101 ? -1 : list.get(0)[1];
22+
}
23+
24+
private int binarySearch(int row, BinaryMatrix binaryMatrix, int right) {
25+
int left = 0;
26+
if (binaryMatrix.get(row, right) == 0) {
27+
return 101;
28+
}
29+
while (left < right) {
30+
int mid = left + (right - left) / 2;
31+
if (binaryMatrix.get(row, mid) == 0) {
32+
left = mid + 1;
33+
} else {
34+
right = mid;
35+
}
36+
}
37+
return left;
38+
}
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.BinaryMatrix;
4+
import com.fishercoder.common.classes.BinaryMatrixImpl;
5+
import com.fishercoder.solutions._1428;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
11+
public class _1428Test {
12+
private static _1428.Solution1 solution1;
13+
private static BinaryMatrix binaryMatrix;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1428.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
binaryMatrix = new BinaryMatrixImpl(new int[][]{
23+
{0, 0},
24+
{1, 1}
25+
});
26+
assertEquals(0, solution1.leftMostColumnWithOne(binaryMatrix));
27+
}
28+
29+
}

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