Print the Matrix after increasing the heights of the blocks
Last Updated :
29 Mar, 2023
Given a 0-based n x n integer matrix where grid[x][y] represents the height of the blocks in row x and column y. You can increase the height of any block (different per block), but the increased height of a block should not change the contour formed. Contour is the view of blocks from a distance in any direction, the task is to print the matrix after increasing the heights of the blocks without disturbing the contour.
Examples:
Input: grid = { { 4, 0, 9, 2 }, { 7, 6, 1, 8 }, { 9, 10, 3, 4 }, { 3, 2, 1, 8 } }
Output:
9 9 9 8
8 8 8 8
9 10 9 8
8 8 8 8
Explanation: The blocks viewed from top or bottom is: [9, 10, 9, 8]
The blocks viewed from left or right is: [9, 8, 10, 8]
Input: grid = { { 0, 0, 1, 2 }, { 1, 1, 1, 1 }, { 0, 0, 0, 1 }, { 1, 1, 1, 0 } }
Output:
1 1 1 2
1 1 1 1
1 1 1 1
1 1 1 1
Explanation: The blocks viewed from top or bottom is: [1, 1, 1, 2]
The blocks viewed from left or right is: [2, 1, 1, 1]
Approach: To solve the problem follow the below idea:
For grid[i][j], it can’t be higher than the maximum of its row nor the maximum of its column. So the maximum increasing height for a block at (i, j) is min row[i], col[j]).
Below are the steps for the above approach:
- Initialize a resultant matrix say result[][] with the given matrix initially, to store the result.
- Initialize arrays say, row and col to store the maximum for each row and column.
- Iterate the matrix and store the maximum and minimum of each row and column in arrays row[] and col[]
- row[i] = max(row[i], grid[i][j])
- col[j] = max(col[j], grid[i][j])
- Run a loop till i = 0 to i < n and a nested loop till j = 0 to j < n and update result[i][j] = min(row[i], col[j]).
- Print the result matrix.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void inc_heights(vector<vector< int > >& grid)
{
int n = grid.size();
vector< int > col(n), row(n);
vector<vector< int > > result(n, vector< int >(n));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
result[i][j] = grid[i][j];
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
row[i] = max(row[i], grid[i][j]);
col[j] = max(col[j], grid[i][j]);
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
result[i][j] = min(row[i], col[j]);
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << result[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
vector<vector< int > > grids = { { 4, 0, 9, 2 },
{ 7, 6, 1, 8 },
{ 9, 10, 3, 4 },
{ 3, 2, 1, 8 } };
inc_heights(grids);
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static void inc_heights( int [][] grid)
{
int n = grid.length;
int [] col = new int [n], row = new int [n];
int [][] result = new int [n][n];
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
result[i][j] = grid[i][j];
}
}
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
row[i] = Math.max(row[i], grid[i][j]);
col[j] = Math.max(col[j], grid[i][j]);
}
}
int res = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
result[i][j] = Math.min(row[i], col[j]);
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
System.out.print(result[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int grids[][] = { { 4 , 0 , 9 , 2 },
{ 7 , 6 , 1 , 8 },
{ 9 , 10 , 3 , 4 },
{ 3 , 2 , 1 , 8 } };
inc_heights(grids);
}
}
|
Python3
def inc_heights(grid):
n = len (grid)
col = [ 0 ] * n
row = [ 0 ] * n
result = [[ 0 ] * n for i in range (n)]
for i in range (n):
for j in range (n):
result[i][j] = grid[i][j]
for i in range (n):
for j in range (n):
row[i] = max (row[i], grid[i][j])
col[j] = max (col[j], grid[i][j])
for i in range (n):
for j in range (n):
result[i][j] = min (row[i], col[j])
for i in range (n):
for j in range (n):
print (result[i][j], end = ' ' )
print ()
if __name__ = = '__main__' :
grids = [[ 4 , 0 , 9 , 2 ],
[ 7 , 6 , 1 , 8 ],
[ 9 , 10 , 3 , 4 ],
[ 3 , 2 , 1 , 8 ]]
inc_heights(grids)
|
C#
using System;
public class GFG {
public static void inc_heights( int [, ] grid)
{
int n = grid.GetLength(0);
int [] col = new int [n], row = new int [n];
int [, ] result = new int [n, n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
result[i, j] = grid[i, j];
}
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
row[i] = Math.Max(row[i], grid[i, j]);
col[j] = Math.Max(col[j], grid[i, j]);
}
}
int res = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
result[i, j] = Math.Min(row[i], col[j]);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
Console.Write(result[i, j] + " " );
}
Console.WriteLine();
}
}
public static void Main( string [] args)
{
int [, ] grids = { { 4, 0, 9, 2 },
{ 7, 6, 1, 8 },
{ 9, 10, 3, 4 },
{ 3, 2, 1, 8 } };
inc_heights(grids);
}
}
|
Javascript
function incHeights(grid) {
const n = grid.length;
const col = new Array(n).fill(0);
const row = new Array(n).fill(0);
const result = new Array(n).fill( null ).map(() => new Array(n).fill( null ));
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
result[i][j] = grid[i][j];
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
row[i] = Math.max(row[i], grid[i][j]);
col[j] = Math.max(col[j], grid[i][j]);
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
result[i][j] = Math.min(row[i], col[j]);
}
}
for (let i = 0; i < n; i++) {
console.log(result[i].join( " " ));
}
}
const grids = [
[4, 0, 9, 2],
[7, 6, 1, 8],
[9, 10, 3, 4],
[3, 2, 1, 8]
];
incHeights(grids);
|
Output
9 9 9 8
8 8 8 8
9 10 9 8
8 8 8 8
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...