Facebook | Leftmost column index of 1

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2019-07-22Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: e3, e4
Interview Stage: unknown
Location: unknown
Year: unknown

Original Post Content:

In a binary matrix (all elements are 0 and 1), every row is sorted in ascending order (0 to the left of 1). Find the leftmost column index with a 1 in it.

Example 1:
Input:
[[0, 0, 0, 1],
[0, 0, 1, 1],
[0, 1, 1, 1],
[0, 0, 0, 0]]
Output: 1

Example 2:
Input:
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
Output: -1

Expected solution better than O(r * c).

Solution
Java: https://leetcode.com/playground/oFDW7DqN
Time complexity: O(r + c).
Space complexity: O(1).

Similar questions
* https://leetcode.com/problems/leftmost-column-with-at-least-a-one/ (premium)
* https://leetcode.com/problems/search-a-2d-matrix-ii

Comments