//https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3306/
        public int LeftMostColumnWithOne(IBinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();
            var min        = int.MaxValue;

            for (int row = 0; row < dimensions[0]; row++)
            {
                var low  = 0;
                var high = dimensions[1] - 1;
                while (low <= high)
                {
                    var mid = (low + high) / 2;
                    if (binaryMatrix.Get(row, mid) == 1)
                    {
                        if (mid == 0)
                        {
                            return(0); // we're done completely. Cant get lower than zero.
                        }
                        else if (binaryMatrix.Get(row, mid - 1) == 0)
                        {
                            min = Math.Min(min, mid);
                            break; //move to next row
                        }
                        else
                        {
                            high = mid - 1;
                        }
                    }
                    else
                    {
                        low = mid + 1;
                    }
                }
            }

            return((min < int.MaxValue) ? min : -1);
        }
Beispiel #2
0
        private int find(int x, int y, int rows, int minIndex, IBinaryMatrix binaryMatrix)
        {
            var val = binaryMatrix.Get(x, y);

            if (val == 0)
            {
                x++;
            }
            else
            {
                minIndex = y;
                y--;
            }

            if (x < rows && y >= 0)
            {
                minIndex = find(x, y, rows, minIndex, binaryMatrix);
            }

            return(minIndex);
        }