Member-only story

Leetcode 1428

Y Tech
Jul 30, 2023

--

Coding question asked in Facebook interviews.

'''
0000000011 1. start from top right corner
0000011111 => 2. keep moving left until reaches a 0
0000000011 3. move down and repeat step 2
0011111111
0000000011

(0, 9) => (0, 7) => (1, 7) => (1, 4) => (2, 4) => (3, 4) => (3, 1)
'''
class Solution:
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
# BinaryMatrix.get(row, col) => value
# BinaryMatrix.dimensions() => [rows, cols]
rows, cols = binaryMatrix.dimensions()
currentR, currentC = 0, cols - 1
while currentR < rows and currentC >= 0:
if binaryMatrix.get(currentR, currentC) == 0:
currentR += 1
else:
currentC -= 1

if currentC == cols - 1:
return -1

return currentC + 1

--

--

Y Tech
Y Tech

No responses yet