Looking at the matrix of dimensions N * m, the task is Maximize The total number of occupied cells in the specified array that follow the specified condition:
If two cells are occupied in the same row, then there must be at least one empty cell between them.
If two cells are occupied in different rows, there must be at least one completely empty row between them That is, if the cells in ith And y row occupied like this I So there must be a file K Completely empty row like that I .
Examples:
entrance: n = 1, m = 5 Produce: 3 Explanation: There is only one row of five seats. A maximum of three cells can be occupied. See the table below where the number 1 indicates the occupied cell.
entrance: n = 3, m = 3 Produce: 4 Explanation: There are three rows with three seats each. The maximum occupied cells can be 4.
naive approach: The problem can be solved using a greedy approach. Start filling from the first row and first column and keep a gap of 1 between any two occupied cells in one row and a one-row gap between two occupied cells of different rows.
Here is the implementation of the above approach:
C++
#include <bits/stdc++.h>
usingnamespacestd;
intsolve(intN, intM)
{
intans = 0;
while(1) {
for(inti = 1; i <= M; i += 2) {
ans++;
}
if(N >= 2) {
N--;
N--;
}
elseif(N == 1) {
N--;
}
if(N == 0) {
break;
}
}
returnans;
}
intmain()
{
intN = 1;
intM = 5;
cout << solve(N, M);
return0;
}
Java
importjava.util.*;
publicclassGFG
{
staticintsolve(intN, intM)
{
intans = 0;
while(true)
{
for(inti = 1; i <= M; i += 2) {
ans++;
}
if(N >= 2) {
N--;
N--;
}
elseif(N == 1) {
N--;
}
if(N == 0) {
break;
}
}
returnans;
}
publicstaticvoidmain(String[] args)
{
intN = 1;
intM = 5;
System.out.print(solve(N, M));
}
}
Python 3
defsolve(N, M):
ans =0;
while(1):
fori inrange(1, M +1, 2):
ans +=1
if(N >=2):
N -=1
N -=1
elif(N ==1):
N -=1
if(N ==0):
break
returnans;
N =1;
M =5;
print(solve(N, M));
bad #
usingSystem;
classGFG {
staticintsolve(intN, intM)
{
intans = 0;
while(true)
{
for(inti = 1; i <= M; i += 2) {
ans++;
}
if(N >= 2) {
N--;
N--;
}
elseif(N == 1) {
N--;
}
if(N == 0) {
break;
}
}
returnans;
}
publicstaticvoidMain()
{
intN = 1;
intM = 5;
Console.Write(solve(N, M));
}
}
JavaScript
<script>
functionsolve(N, M)
{
let ans = 0;
while(1)
{
for(let i = 1; i <= M; i += 2) {
ans++;
}
if(N >= 2) {
N--;
N--;
}
elseif(N == 1) {
N--;
}
if(N == 0) {
break;
}
}
returnans;
}
let N = 1;
let M = 5;
document.write(solve(N, M));
</script>
time complexity:O (N * M) Additional space: oh (1)
Active approach: To maximize the total number of occupied cells, they should be filled in the above manner. The total number can be obtained from the following observation:
So, the maximum number of cells that can be occupied per row is ceiling (m / 2). Since there is a single row gap between any two occupied cells of different rows, The maximum number of rows that can be filled is Ceiling (N/2). And therefore Maximum The number of cells that can be occupied is Ceiling (m/2) * Ceiling (N/2).
Below is the implementation of the above approach.