Beispiel #1
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 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);
        }
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);
            }
        }
    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 #5
0
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        var dimensions = binaryMatrix.Dimensions();
        var rows       = dimensions[0];
        var cols       = dimensions[1];

        var minLeft = -1;

        for (var i = 0; i < rows; i++)
        {
            var leftMostIndex = GetNonZeroLeftIndex(i, cols, binaryMatrix);
            if (leftMostIndex != -1)
            {
                if (minLeft == -1)
                {
                    minLeft = leftMostIndex;
                }
                else
                {
                    minLeft = Math.Min(leftMostIndex, minLeft);
                }
            }
        }

        return(minLeft);
    }
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        IList <int> dims = binaryMatrix.Dimensions();

        NumRows = dims[0];

        Find(binaryMatrix, 0, dims[1] - 1);
        return(MinIndex);
    }
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        var dims = binaryMatrix.Dimensions();
        var rows = dims[0];
        var cols = dims[1];
        var idx  = int.MaxValue;

        for (var row = 0; row < rows; ++row)
        {
            var idxRow = FindIdx(binaryMatrix, row, cols);
            if (idxRow != -1)
            {
                idx = Math.Min(idx, idxRow);
            }
        }

        return(idx == int.MaxValue ? -1 : idx);
    }
Beispiel #8
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 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 #10
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);
        }
Beispiel #11
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);
        }
        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
        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);
        }
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);
    }
Beispiel #16
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);
            }
        }