📌  相关文章
📜  在按行排序的二进制矩阵中,至少有一个1的最左列|套装2

📅  最后修改于: 2021-04-24 04:38:43             🧑  作者: Mango

给定包含0和1的二进制矩阵mat [] [] 。矩阵的每一行都以非降序排列,任务是找到矩阵的最左列,其中至少有一个1。

注意:如果不存在这样的列,则返回-1。

例子:

Input: 
mat[][] = {{0, 0, 0, 1}
           {0, 1, 1, 1}
           {0, 0, 1, 1}}
Output: 2
Explanation:
The 2nd column of the
matrix contains atleast a 1.

Input: 
mat[][] = {{0, 0, 0}
           {0, 1, 1}  
           {1, 1, 1}}
Output: 1
Explanation:
The 1st column of the
matrix contains atleast a 1.

Input: 
mat[][] = {{0, 0}
           {0, 0}}
Output: -1
Explanation:
There is no such column which 
contains atleast one 1.

方法:

  • 在这里,我们从第一行的最后一个元素开始遍历。这包括两个步骤。
    1. 如果当前迭代元素为1,则我们递减列索引。当我们发现最左边的列索引值为1时,就不必检查具有更大列索引的元素。
    2. 如果当前迭代元素为0,则我们增加行索引。由于该元素为0,因此我们不需要检查该行的先前元素。
  • 我们继续操作,直到行或列索引之一变为无效。

下面是上述方法的实现。

C++
// C++ program to calculate leftmost 
// column having at least a 1
#include 
using namespace std;
  
#define N 3
#define M 4
  
// Function return leftmost 
// column having at least a 1
int FindColumn(int mat[N][M])
{
    int row = 0, col = M - 1;
    int flag = 0;
  
    while (row < N && col >= 0)
    {
        // If current element is
        // 1 decrement column by 1
        if (mat[row][col] == 1)
        {
            col--;
            flag = 1;
        }
        // If current element is
        // 0 increment row by 1
        else
        {
            row++;
        }
    }
  
    col++;
      
    if (flag)
        return col + 1;
    else
        return -1;
}
  
// Driver code
int main()
{
    int mat[N][M] = { { 0, 0, 0, 1 },
                      { 0, 1, 1, 1 },
                      { 0, 0, 1, 1 } };
  
    cout << FindColumn(mat);
  
    return 0;
}


Java
// Java program to calculate leftmost 
// column having at least a 1
import java.util.*;
  
class GFG{
  
static final int N = 3;
static final int M = 4;
  
// Function return leftmost 
// column having at least a 1
static int FindColumn(int mat[][])
{
    int row = 0, col = M - 1;
    int flag = 0;
  
    while(row < N && col >= 0)
    {
        // If current element is
        // 1 decrement column by 1
        if(mat[row][col] == 1)
        {
           col--;
           flag = 1;
        }
        // If current element is
        // 0 increment row by 1
        else
        {
            row++;
        }
    }
    col++;
  
    if (flag!=0)
        return col + 1;
    else
        return -1;
}
  
// Driver code
public static void main(String[] args)
{
    int[][] mat = { { 0, 0, 0, 1 },
                    { 0, 1, 1, 1 },
                    { 0, 0, 1, 1 } };
                      
    System.out.print(FindColumn(mat));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to calculate leftmost
# column having at least a 1
N = 3
M = 4
  
# Function return leftmost
# column having at least a 1
def findColumn(mat: list) -> int:
    row = 0
    col = M - 1
  
    while row < N and col >= 0:
  
        # If current element is
        # 1 decrement column by 1
        if mat[row][col] == 1:
            col -= 1
            flag = 1
  
        # If current element is
        # 0 increment row by 1
        else:
            row += 1
              
    col += 1
  
    if flag:
        return col + 1
    else:
        return -1
  
# Driver Code
if __name__ == "__main__":
      
    mat = [ [0, 0, 0, 1],
            [0, 1, 1, 1],
            [0, 0, 1, 1] ]
                
    print(findColumn(mat))
  
# This code is contributed by sanjeev2552


C#
// C# program to calculate leftmost 
// column having at least 1
using System;
  
class GFG{
  
static readonly int N = 3;
static readonly int M = 4;
  
// Function return leftmost 
// column having at least a 1
static int FindColumn(int [,]mat)
{
    int row = 0, col = M - 1;
    int flag = 0;
  
    while(row < N && col >= 0)
    {
          
        // If current element is
        // 1 decrement column by 1
        if (mat[row, col] == 1)
        {
            col--;
            flag = 1;
        }
          
        // If current element is
        // 0 increment row by 1
        else
        {
            row++;
        }
    }
    col++;
  
    if (flag != 0)
        return col + 1;
    else
        return -1;
}
  
// Driver code
public static void Main(String[] args)
{
    int[,] mat = { { 0, 0, 0, 1 },
                   { 0, 1, 1, 1 },
                   { 0, 0, 1, 1 } };
                      
    Console.Write(FindColumn(mat));
}
}
  
// This code is contributed by Rohit_ranjan


输出:
2

时间复杂度: O(N + M)。其中N是行数,M是列数。
空间复杂度: O(1)