给定一个维度为M x N的二进制矩阵mat[][]和两人P1, P2 ,任务是找到最后完成从矩阵中选择0 的人,该矩阵仅当行或列变为1时由0组成的单元格在矩阵中有一个或多个1 。
注意: P1 先开始取0 ,两个人都想最后完成。给定的矩阵将始终至少有一个0可以选择。
例子:
Input: mat[][] = {{1, 0, 0}, {0, 0, 0}, {0, 0, 1}}
Output: P1
Explanation:
P1 chooses mat[1][1], then the matrix becomes {{1, 0, 0}, {0, 1, 0}, {0, 0,1}}.
P2 has no 0 left to choose from. So, P1 finishes last.
Input: mat[][] = {{0, 0}, {0, 0}}
Output: P2
Explanation:
No matter P1 chooses which 0 P2 will always have a 0 to choose and
after P2 picks a 0 there will not be any other 0 to choose from.
方法:这个想法是基于这样的观察,一个0不能采取如果任其行或列的有1.按照下面的步骤来解决这个问题:
- 初始化两组,行&COLS计数行和列,其不包含任何1的数量。
- 遍历矩阵并在集合中添加具有1 的行和列。
- 取最小的行数或列数,就好像它们中的任何一个变为零一样,这样就不能再取0了。
- 在找到可用的最小行数和列数后,如果所做的选择数为奇数,则P1最后完成,否则P2最后完成。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the person
// who will finish last
void findLast(int mat[][3])
{
int m = 3;
int n = 3;
// To keep track of rows
// and columns having 1
set rows;
set cols;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j]) {
rows.insert(i);
cols.insert(j);
}
}
}
// Available rows and columns
int avRows = m - rows.size();
int avCols = n - cols.size();
// Minimum number of choices we have
int choices = min(avRows, avCols);
// If number of choices are odd
if (choices & 1)
// P1 will finish last
cout << "P1";
// Otherwise, P2 will finish last
else
cout << "P2";
}
// Given matrix
int main()
{
int mat[][3]
= { { 1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 } };
findLast(mat);
}
// This code is contributed by ukasp.
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to find the person
// who will finish last
static void findLast(int mat[][])
{
int m = 3;
int n = 3;
// To keep track of rows
// and columns having 1
Set rows = new HashSet();
Set cols = new HashSet();
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if ((mat[i][j] > 0))
{
rows.add(i);
cols.add(j);
}
}
}
// Available rows and columns
int avRows = m - rows.size();
int avCols = n - cols.size();
// Minimum number of choices we have
int choices = Math.min(avRows, avCols);
// If number of choices are odd
if ((choices & 1) != 0)
// P1 will finish last
System.out.println("P1");
// Otherwise, P2 will finish last
else
System.out.println("P2");
}
// Driver code
public static void main (String[] args)
{
int mat[][] = { { 1, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 1 } };
findLast(mat);
}
}
// This code is contributed by jana_sayantan
Python3
# Python3 program for the above approach
# Function to find the person
# who will finish last
def findLast(mat):
m = len(mat)
n = len(mat[0])
# To keep track of rows
# and columns having 1
rows = set()
cols = set()
for i in range(m):
for j in range(n):
if mat[i][j]:
rows.add(i)
cols.add(j)
# Available rows and columns
avRows = m-len(list(rows))
avCols = n-len(list(cols))
# Minimum number of choices we have
choices = min(avRows, avCols)
# If number of choices are odd
if choices & 1:
# P1 will finish last
print('P1')
# Otherwise, P2 will finish last
else:
print('P2')
# Given matrix
mat = [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
findLast(mat)
Javascript
P1
时间复杂度: O(M*N)
辅助空间: O(M*N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live