Skip to content

Commit 0e0cce5

Browse files
author
Partho Biswas
committed
1428_Leftmost_Column_with_at_Least_a_One
1 parent 0ef0f8e commit 0e0cce5

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ I have solved quite a number of problems from several topics. See the below tabl
196196
|82| **[766. Toeplitz Matrix](https://tinyurl.com/s3z4lem)** | [Python](https://tinyurl.com/wu6rdaw/766_Toeplitz_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/766_Toeplitz_Matrix.swift) | --- | Easy | --- |
197197
|83| **[1031. Maximum Sum of Two Non-Overlapping Subarrays](https://tinyurl.com/y3x46a5t)** | [Python](https://tinyurl.com/wu6rdaw/1031_Maximum_Sum_of_Two_Non_Overlapping_Subarrays.py), [Swift](https://tinyurl.com/wuja3c4/1031_Maximum_Sum_of_Two_Non_Overlapping_Subarrays.swift) | --- | Medium | PrefixSum |
198198
|84| **[1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://tinyurl.com/yysmvobr)** | [Python](https://tinyurl.com/wu6rdaw/1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit.py), [Swift](https://tinyurl.com/wuja3c4/1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit.swift) | (Art 1)[https://tinyurl.com/y69hjhjp], (Art 2)[https://tinyurl.com/y3h2fj2m] | Medium | Sliding window |
199+
|85| **[1428. Leftmost Column with at Least a One](https://tinyurl.com/y3reu3y2)** | [Python](https://tinyurl.com/wu6rdaw/1428_Leftmost_Column_with_at_Least_a_One.py), [Swift](https://tinyurl.com/wuja3c4/1428_Leftmost_Column_with_at_Least_a_One.swift) | --- | Medium | Binary Search |
199200

200201

201202
</p>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* // This is the BinaryMatrix's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* public class BinaryMatrix {
5+
* public func get(_ row: Int, _ col: Int) -> Int {}
6+
* public func dimensions() -> [Int] {}
7+
* };
8+
*/
9+
10+
class Solution {
11+
func leftMostColumnWithOne(_ binaryMatrix: BinaryMatrix) -> Int {
12+
let (rows, cols) = (binaryMatrix.dimensions()[0], binaryMatrix.dimensions()[1])
13+
var leftMostCol = Int.max
14+
for row in 0..<rows {
15+
var (leftCol, rightCol) = (0, cols - 1)
16+
while leftCol <= rightCol {
17+
let midCol: Int = leftCol + (rightCol - leftCol) / 2
18+
if binaryMatrix.get(row, midCol) == 1 {
19+
leftMostCol = min(leftMostCol, midCol)
20+
rightCol = midCol - 1
21+
} else {
22+
leftCol = midCol + 1
23+
}
24+
}
25+
}
26+
return leftMostCol == Int.max ? -1 : leftMostCol
27+
}
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Foundation
2+

0 commit comments

Comments
 (0)