在满足条件的给定矩阵中最大化占用的单元格
给定一个维度为N*M的矩阵,任务是最大化给定矩阵中被占用单元的总数,使得它们遵循给定条件:
- 如果同一行中有两个单元格,则它们之间必须至少有一个空单元格。
- 如果两个单元格被占用在不同的行中,则它们之间必须至少有一个完全空的行
即,如果第 i行和第 j行中的单元格被占用,使得i < j则必须有第 k行完全空,使得i < k < j 。
例子:
Input: N = 1 ,M = 5
Output: 3
Explanation: There is only one row with five seats.
Maximum three cells can be occupied.
See the table below where 1 denotes occupied cell.1 1 1
Input: N = 3 ,M = 3
Output: 4
Explanation: There are three rows with three seats each.
Maximum occupied cells can be 4.
1 | 1 | |
1 | 1 |
朴素方法:可以使用贪心方法解决问题。从第一行第一列开始填充,保持一行任意两个占用单元格之间的间隙为1,不同行的两个占用单元格之间保持一行的间隙。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find
// maximum number of occupied cells
int solve(int N, int M)
{
int ans = 0;
while (1) {
// Count number of occupied cells
// in one row
for (int i = 1; i <= M; i += 2) {
ans++;
}
// If row numbers to be filled
// are greater than or equal to 2
// decrease by 2 otherwise decrease by 1
// and if number of rows to be filled
// are 0 return ans
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break;
}
}
// Return number of occupied cells
return ans;
}
// Driver code
int main()
{
int N = 1;
int M = 5;
cout << solve(N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find
// maximum number of occupied cells
static int solve(int N, int M)
{
int ans = 0;
while (true)
{
// Count number of occupied cells
// in one row
for (int i = 1; i <= M; i += 2) {
ans++;
}
// If row numbers to be filled
// are greater than or equal to 2
// decrease by 2 otherwise decrease by 1
// and if number of rows to be filled
// are 0 return ans
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break;
}
}
// Return number of occupied cells
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 1;
int M = 5;
System.out.print(solve(N, M));
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above approach
# Function to find
# maximum number of occupied cells
def solve(N, M):
ans = 0;
while (1):
# Count number of occupied cells
# in one row
for i in range(1, M + 1, 2):
ans += 1
# If row numbers to be filled
# are greater than or equal to 2
# decrease by 2 otherwise decrease by 1
# and if number of rows to be filled
# are 0 return ans
if (N >= 2):
N -= 1
N -= 1
elif (N == 1):
N -= 1
if (N == 0):
break
# Return number of occupied cells
return ans;
# Driver code
N = 1;
M = 5;
print(solve(N, M));
# This code is contributed by saurabh_jaiswal.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find
// maximum number of occupied cells
static int solve(int N, int M)
{
int ans = 0;
while (true)
{
// Count number of occupied cells
// in one row
for (int i = 1; i <= M; i += 2) {
ans++;
}
// If row numbers to be filled
// are greater than or equal to 2
// decrease by 2 otherwise decrease by 1
// and if number of rows to be filled
// are 0 return ans
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break;
}
}
// Return number of occupied cells
return ans;
}
// Driver code
public static void Main()
{
int N = 1;
int M = 5;
Console.Write(solve(N, M));
}
}
// This code is contributed by ukasp.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find
// maximum number of occupied cells
int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
int main()
{
int N = 1;
int M = 5;
cout << solve(N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find
// maximum number of occupied cells
public static int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 1;
int M = 5;
System.out.print(solve(N, M));
}
}
// This code is contributed by Shubham Singh
Python3
# Python program for the above approach
# Function to find
# maximum number of occupied cells
def solve (N, M):
x = (M + 1) // 2
y = (N + 1) // 2
ans = x * y
# Return number of occupied cells
return ans
# Driver code
N = 1
M = 5
print(solve(N, M));
# This code is contributed by Shubham Singh
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find
// maximum number of occupied cells
public static int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
public static void Main(String[] args)
{
int N = 1;
int M = 5;
Console.Write(solve(N, M));
}
}
// This code is contributed by shikhasingrajput
Javascript
3
时间复杂度: O(N*M)
辅助空间: O(1)
高效方法:为了最大化占用单元的总数,需要以上述方式占用它们。总数可以从以下观察中获得:
So, the maximum number of cells that can be occupied for each row is ceil(M/2).
And as there is a gap of one row between any two occupied cells of different rows,
the maximum number of rows that can be occupied is ceil(N/2).
Therefore maximum number of cells that can be occupied is ceil(M/2) * ceil(N/2).
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find
// maximum number of occupied cells
int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
int main()
{
int N = 1;
int M = 5;
cout << solve(N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find
// maximum number of occupied cells
public static int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 1;
int M = 5;
System.out.print(solve(N, M));
}
}
// This code is contributed by Shubham Singh
Python3
# Python program for the above approach
# Function to find
# maximum number of occupied cells
def solve (N, M):
x = (M + 1) // 2
y = (N + 1) // 2
ans = x * y
# Return number of occupied cells
return ans
# Driver code
N = 1
M = 5
print(solve(N, M));
# This code is contributed by Shubham Singh
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find
// maximum number of occupied cells
public static int solve(int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
// Return number of occupied cells
return ans;
}
// Driver code
public static void Main(String[] args)
{
int N = 1;
int M = 5;
Console.Write(solve(N, M));
}
}
// This code is contributed by shikhasingrajput
Javascript
3
时间复杂度: O(1)
辅助空间: O(1)