public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var nm      = binaryMatrix.Dimensions();
            var columns = nm[0];
            var rows    = nm[1];

            var hasBoth = 0;
            var ret     = 0;

            for (int x = 0; x < rows; x++)
            {
                for (int y = 0; y < columns; y++)
                {
                    hasBoth += binaryMatrix.Get(y, x);

                    if (hasBoth > 0)
                    {
                        return(ret);
                    }
                }

                ret++;
            }

            return(-1);
        }
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        var result     = -1;
        var dimensions = binaryMatrix.Dimensions();
        var row        = 0;
        var col        = dimensions[1] - 1;

        while (dimensions[0] > row && 0 <= col)
        {
            var cell = binaryMatrix.Get(row, col);

            if (0 == cell)
            {
                ++row;
            }
            else
            {
                result = col;
                --col;
            }
        }


        return(result);
    }
Beispiel #3
0
        /// <summary>
        /// Leftmost Column with at Least a One (Mine)
        /// </summary>
        /// <param name="binaryMatrix"></param>
        /// <returns></returns>
        public static int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var list = binaryMatrix.Dimensions();
            int m = list[0], n = list[1];
            int st = 0, end = n - 1, res = int.MaxValue;

            while (st <= end)
            {
                int mid = (st + end) / 2;
                if (RightColumn(mid))
                {
                    end = mid - 1;
                    res = mid;
                }
                else
                {
                    st = mid + 1;
                }
            }

            return(res == int.MaxValue ? -1 : res);

            bool RightColumn(int col)
            {
                for (int i = 0; i < m; i++)
                {
                    if (binaryMatrix.Get(i, col) == 1)
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
Beispiel #4
0
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        List <int> dim = binaryMatrix.Dimensions().ToList();

        int rows = dim[0], columns = dim[1];
        int x = 0, y = columns - 1;

        if (rows == 0 || columns == 0)
        {
            return(-1);
        }
        int result = -1;

        while (x < rows && y >= 0)
        {
            int val = binaryMatrix.Get(x, y);
            if (val == 1)
            {
                result = y;
                y--;
            }
            else
            {
                x++;
            }
        }
        return(result);
    }
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();
            int?currentMin = null;

            for (int i = 0; i < dimensions[0]; i++)//rows
            {
                if (binaryMatrix.Get(i, dimensions[1] - 1) == 0)
                {
                    continue;
                }

                int start = 0, end = currentMin.HasValue ? currentMin.Value : dimensions[1] - 1, mid;
                while (start <= end)
                {
                    if (binaryMatrix.Get(i, end) != 1 || start < 0 || end >= dimensions[1])
                    {
                        break;
                    }

                    mid = start + (end - start) / 2;

                    if (binaryMatrix.Get(i, start) == 1)
                    {
                        currentMin = start;
                        break;
                    }

                    if (binaryMatrix.Get(i, mid) == 1)
                    {
                        currentMin = mid;
                        end        = mid - 1;
                    }
                    else
                    {
                        currentMin = end;
                        start      = mid + 1;
                    }
                }
            }

            return(currentMin.HasValue ? currentMin.Value : -1);
        }
Beispiel #6
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var size   = binaryMatrix.Dimensions();
            var result = -1;
            var row    = size[0] - 1;
            var col    = size[1] - 1;

            while (row >= 0 && col >= 0)
            {
                if (binaryMatrix.Get(row, col) == 0)
                {
                    row--;
                }
                else if (binaryMatrix.Get(row, col) == 1)
                {
                    result = col;
                    col--;
                }
            }

            return(result);
        }
Beispiel #7
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();

            var row = 0;
            var col = dimensions[1] - 1;

            while (row < dimensions[0])
            {
                while (col >= 0 && binaryMatrix.Get(row, col) == 1)
                {
                    col--;
                }
                row++;
            }

            return(col + 1 == dimensions[1] ? -1 : col + 1);
        }
    public void Find(BinaryMatrix bm, int r, int c)
    {
        int v = bm.Get(r, c);

        if (v == 1)
        {
            MinIndex = c;
        }

        if (v == 0 && r < NumRows - 1)
        {
            Find(bm, r + 1, c);
        }

        if (v == 1 && c > 0)
        {
            Find(bm, r, c - 1);
        }
    }
Beispiel #9
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();
            var result     = -1;
            var x          = 0;
            var y          = dimensions[1] - 1;

            while (x < dimensions[0] && y >= 0)
            {
                if (binaryMatrix.Get(x, y) == 0)
                {
                    x++;
                }
                else
                {
                    result = y--;
                }
            }
            return(result);
        }
    private static int FindIdx(BinaryMatrix binaryMatrix, int row, int cols)
    {
        var lo = 0;
        var hi = cols - 1;

        while (lo <= hi)
        {
            var m   = lo + (hi - lo) / 2;
            var val = binaryMatrix.Get(row, m);

            if (val == 0)
            {
                lo = m + 1;
            }
            else
            {
                hi = m - 1;
            }
        }

        return(lo < cols ? lo : -1);
    }
Beispiel #11
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            IList <int> dimensions = binaryMatrix.Dimensions();
            int         rows       = dimensions[0];
            int         cols       = dimensions[1];

            int leftMost = cols;

            for (int i = 0; i < rows; i++)
            {
                if (binaryMatrix.Get(i, cols - 1) == 0)
                {
                    continue;
                }
                int minCol = FindLeftMostColumn(binaryMatrix, i, cols);
                if (minCol < leftMost)
                {
                    leftMost = minCol;
                }
            }
            return(leftMost == cols ? -1 : leftMost);
        }
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimentions = binaryMatrix.Dimensions();
            var rowIndex   = 0;
            var colIndex   = dimentions[1] - 1;

            while (colIndex >= 0 && rowIndex < dimentions[0])
            {
                var matrixValue = binaryMatrix.Get(rowIndex, colIndex);

                if (matrixValue == 1)
                {
                    colIndex--;
                }
                else
                {
                    rowIndex++;
                }
            }

            return(colIndex == dimentions[1] - 1 ? -1 : colIndex + 1);
        }
Beispiel #13
0
    private int GetNonZeroLeftIndex(int i, int cols, BinaryMatrix matrix)
    {
        var left         = 0;
        var right        = cols;
        var firstNonZero = -1;

        while (left <= right)
        {
            var mid = left + (right - left) / 2;

            if (matrix.Get(i, mid) == 0)
            {
                left = mid + 1;
            }
            else
            {
                right        = mid - 1;
                firstNonZero = mid;
            }
        }

        return(firstNonZero);
    }
Beispiel #14
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dia          = binaryMatrix.Dimensions();
            int r            = dia[0] - 1;
            int c            = dia[1] - 1;
            int lastClouindx = c;

            for (int i = 0; i < r; i++)
            {
                for (int j = lastClouindx; j > 0; j--)
                {
                    if (binaryMatrix.Get(i, j) == 1)
                    {
                        lastClouindx = j;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(lastClouindx == c?-1: lastClouindx + 1);
        }
Beispiel #15
0
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        IList <int> dim    = binaryMatrix.Dimensions();
        int         row    = dim[0];
        int         column = dim[1];

        int result = -1;
        int r      = 0;
        int c      = column - 1;

        while (r < row && c >= 0)
        {
            if (binaryMatrix.Get(r, c) == 1)
            {
                result = c;
                c--;
            }
            else
            {
                r++;
            }
        }
        return(result);
    }
        static BinaryMatrix GenerateHWithIdentity(BinaryMatrix H)
        {
            BinaryMatrix HWithIdentity = new BinaryMatrix(H.RowAmount, H.ColumnAmount + H.RowAmount);

            for (int y = 0; y < H.RowAmount; y++)
            {
                for (int x = 0; x < H.ColumnAmount; x++)
                {
                    HWithIdentity.Set(y, x, H.Get(y, x));
                }
            }

            for (int y = 0; y < H.RowAmount; y++)
            {
                int n = 0;
                for (int x = H.ColumnAmount; x < H.ColumnAmount + H.RowAmount; x++)
                {
                    HWithIdentity.Set(y, x, y == n);

                    n++;
                }
            }
            return(HWithIdentity);
        }
Beispiel #17
0
        private int FindLeftMostColumn(BinaryMatrix binaryMatrix, int i, int cols)
        {
            int start = 0;
            int end   = cols - 2;
            int min   = cols - 1;

            while (start <= end)
            {
                int mid = (start + end) / 2;
                if (binaryMatrix.Get(i, mid) == 1)
                {
                    if (mid < min)
                    {
                        min = mid;
                    }
                    end = mid - 1;
                }
                else
                {
                    start = mid + 1;
                }
            }
            return(min);
        }
Beispiel #18
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            /////Use the API interface #1
            List <int> x = (List <int>)binaryMatrix.Dimensions();

            //start from top & right
            int currentrow = x[0] - 1;
            int currentcol = x[1] - 1;

            int leftmost = -1;

            while (currentcol >= 0 && currentrow >= 0)
            {
                ////Use the API Interface #2
                int temp = binaryMatrix.Get(currentrow, currentcol);

                if (temp == 1) //Move left while it is ONE
                {
                    leftmost = currentcol;
                    currentcol--;
                }
                else //Move down if run into Zero
                {
                    currentrow--;
                }
            }

            if (currentcol == -1)//run out of the matrix from left side
            {
                return(0);
            }
            else    //run out of the matrix from bottom................
            {
                return(leftmost);
            }
        }