← Home

Leftmost Column with at Least a One

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
binary searchC++Markdown
LC

This problem looks busy at first, but the accepted solution is built around one steady invariant. For Leftmost Column with at Least a One, the solution in this repository is mainly a binary search solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: binary search, two pointers, sliding window, greedy.

The notes already sitting in the source point us in the right direction:

  • Binary Search

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are get, dimensions, leftMostColumnWithOne.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Binary Search
02/**
03 * // This is the BinaryMatrix's API interface.
04 * // You should not implement it, or speculate about its implementation
05 * class BinaryMatrix {
06 *   public:
07 *     int get(int x, int y);
08 *     vector<int> dimensions();
09 * };
10 */
11
12class Solution {
13public:
14    int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) {
15        vector<int> nm = binaryMatrix.dimensions();
16        int n = nm[0], m = nm[1];
17        int ans = INT_MAX;
18        
19        //n * m matrix
20        for(int i = 0; i < n; i++){
21            //binary search on each row
22            int l = 0, r = m-1;
23            while(l < r){
24                int mid = l + (r-l)/2;
25                if(binaryMatrix.get(i, mid) == 0){
26                    //search right part
27                    l = mid+1;
28                }else{
29                    //search left part
30                    //retain the 1 met
31                    r = mid;
32                }
33            }
34            //now r is the index of leftmost 1
35            if(binaryMatrix.get(i, r) == 1)
36                ans = min(ans, r);
37        }
38        
39        return (ans == INT_MAX) ? -1 : ans;
40    }
41};
42
43//Hint 2, greedy
44/**
45 * // This is the BinaryMatrix's API interface.
46 * // You should not implement it, or speculate about its implementation
47 * class BinaryMatrix {
48 *   public:
49 *     int get(int x, int y);
50 *     vector<int> dimensions();
51 * };
52 */
53
54class Solution {
55public:
56    int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) {
57        vector<int> nm = binaryMatrix.dimensions();
58        int n = nm[0], m = nm[1];
59        
60        int i = 0, j = m-1;
61        int ans = INT_MAX;
62        
63        while(i < n && j >= 0){
64            if(binaryMatrix.get(i, j) == 0){
65                i++;
66            }else{
67                ans = min(ans, j);
68                j--;
69            }
70        }
71        
72        return (ans == INT_MAX) ? -1 : ans;
73    }
74};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.