public int LeftMostColumnWithOne(IBinaryMatrix binaryMatrix) { var rows = binaryMatrix.Dimensions()[0]; var cols = binaryMatrix.Dimensions()[1]; return(find(0, cols - 1, rows, -1, binaryMatrix)); }
//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); }