You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 23, 2025. It is now read-only.
Write an efficient algorithm that searches for a value in an m x n matrix.
This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer
of the previous row
Input:
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
An image is represented by an m x n integer grid image
where image[i][j] represents the pixel value of the image.
You are also given three integers sr, sc, and newColor.
You should perform a flood fill on the image starting
from the pixel image[sr][sc].
To perform a flood fill, consider the starting pixel,
plus any pixels connected 4-directionally to the starting pixel
of the same color as the starting pixel,
plus any pixels connected 4-directionally to those pixels
(also with the same color), and so on.
Replace the color of all of the aforementioned pixels with newColor.
Return the modified image after performing the flood fill.
classSolution {
publicint[][] floodFill(int[][] image, intsr, intsc, intnewColor) {
intcolor = image[sr][sc];
if (color != newColor)
dfs(image, sr, sc, color, newColor);
returnimage;
}
publicvoiddfs(int[][] image, intr, intc, intcolor, intnewColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r >= 1)
dfs(image, r - 1, c, color, newColor);
if (c >= 1)
dfs(image, r, c - 1, color, newColor);
if (r + 1 < image.length)
dfs(image, r + 1, c, color, newColor);
if (c + 1 < image[0].length)
dfs(image, r, c + 1, color, newColor);
}
}
}
Max Area of Island
You are given an m x n binary matrix grid.
An island is a group of 1's (representing land) connected 4-directionally
(horizontal or vertical.) You may assume all four edges of the
grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
classSolution {
int[][] grid;
boolean[][] seen;
publicintarea(intr, intc) {
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length ||
seen[r][c] || grid[r][c] == 0)
return0;
seen[r][c] = true;
return (1 + area(r + 1, c) + area(r - 1, c)
+ area(r, c - 1) + area(r, c + 1));
}
publicintmaxAreaOfIsland(int[][] grid) {
this.grid = grid;
seen = newboolean[grid.length][grid[0].length];
intans = 0;
for (intr = 0; r < grid.length; r++) {
for (intc = 0; c < grid[0].length; c++) {
ans = Math.max(ans, area(r, c));
}
}
returnans;
}
}
Reshape the Matrix
InMATLAB, thereisahandyfunctioncalledreshapewhichcanreshapeanmxnmatrixintoanewonewithadifferentsizerxckeepingitsoriginaldata.
Youaregivenanmxnmatrixmatandtwointegersrandcrepresentingthenumberofrowsandthenumberofcolumnsofthewantedreshapedmatrix.
Input: mat = [[1,2],[3,4]], r = 1, c = 4Output: [[1,2,3,4]
classSolution {
publicint[][] matrixReshape(int[][] mat, intr, intc) {
intn = mat.length;
intm = mat[0].length;
if ((n * m) != (r * c))
returnmat;
int[][] ans = newint[r][c];
intk = 0, l = 0;
for (inti = 0; i < n; i++) {
for (intj = 0; j < m; j++) {
if (l >= c) {
l = 0;
k++;
}
ans[k][l++] = mat[i][j];
}
}
returnans;
}
}
Input:
R = 4, C = 3
matrix[][] = {
{ 1, 0, 0},
{ 1, 0, 0},
{ 1, 0, 0},
{ 0, 0, 0}
}
Output:
1 1 1
1 1 1
1 1 1
1 0 0
Explanation:
The position of cells that have 1 in
the original matrix are (0,0), (1,0)
and (2,0). Therefore, all cells in row
0,1,2 are and column 0 are modified to 1.
classSolution {
publicstaticvoidmodifyMatrix(intmat[][]) {
// variables to check if there are any 1// in first row and columnbooleanrow_flag = false;
booleancol_flag = false;
// updating the first row and col if 1// is encounteredfor (inti = 0; i < mat.length; i++) {
for (intj = 0; j < mat[0].length; j++) {
if (i == 0 && mat[i][j] == 1)
row_flag = true;
if (j == 0 && mat[i][j] == 1)
col_flag = true;
if (mat[i][j] == 1) {
mat[0][j] = 1;
mat[i][0] = 1;
}
}
}
// Modify the input matrix mat[] using the// first row and first column of Matrix matfor (inti = 1; i < mat.length; i++) {
for (intj = 1; j < mat[0].length; j++) {
if (mat[0][j] == 1 || mat[i][0] == 1) {
mat[i][j] = 1;
}
}
}
// modify first row if there was any 1if (row_flag == true) {
for (inti = 0; i < mat[0].length; i++) {
mat[0][i] = 1;
}
}
// modify first col if there was any 1if (col_flag == true) {
for (inti = 0; i < mat.length; i++) {
mat[i][0] = 1;
}
}
}
/* A utility function to print a 2D matrix */publicstaticvoidprintMatrix(intmat[][]) {
for (inti = 0; i < mat.length; i++) {
for (intj = 0; j < mat[0].length; j++) {
System.out.print(mat[i][j]);
}
System.out.println("");
}
}
// Driver function to test the above functionpublicstaticvoidmain(Stringargs[]) {
intmat[][] = {
{ 1, 0, 0, 1 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
};
System.out.println("Input Matrix :");
printMatrix(mat);
modifyMatrix(mat);
System.out.println("Matrix After Modification :");
printMatrix(mat);
}
}
Make matrix beautiful
Input:
N = 3
matrix[][] = {
{1, 2, 3},
{4, 2, 3},
{3, 2, 1}
}
Output: 6
Explanation:
Original matrix is as follows:
1 2 3
4 2 3
3 2 1
We need to make increment in such a way
that each row and column has one time 2,
one time 3 , one time 4. For that we
need 6 operations
importjava.util.Arrays;
classSolution {
// Function to find minimum number of operations that are required// to make the matrix beautiful.staticintfindMinOperation(intmatrix[][], intn)
{
intsumRow[] = newint[n];
intsumCol[] = newint[n];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
//calculating sumRow[] and sumCol[] array.for(inti = 0; i < n; i++)
{
for(intj = 0; j < n; j++)
{
sumRow[i] += matrix[i][j];
sumCol[j] += matrix[i][j];
}
}
//finding maximum sum value in either row or in column.intmaxSum = 0;
for (inti = 0; i < n; ++i)
{
maxSum = Math.max(maxSum, sumRow[i]);
maxSum = Math.max(maxSum, sumCol[i]);
}
intcount = 0;
for (inti = 0, j = 0; i < n && j < n;)
{
//finding minimum increment required in either row or column.intdiff = Math.min(maxSum - sumRow[i], maxSum - sumCol[j]);
//adding difference in corresponding cell, //sumRow[] and sumCol[] array.matrix[i][j] += diff;
sumRow[i] += diff;
sumCol[j] += diff;
//updating the result.count += diff;
//if ith row is satisfied, incrementing i for next iteration.if (sumRow[i] == maxSum)
++i;
//if jth column is satisfied, incrementing j for next iteration.if (sumCol[j] == maxSum)
++j;
}
//returning the result.returncount;
}
Unique rows in boolean matrix
Input:
row = 3, col = 4
M[][] = {
{1 1 0 1},
{1 0 0 1},
{1 1 0 1}
}
Output: '1 1 0 1' & '1 0 0 1'
Explanation:
Above the matrix of size 3x4 looks like
1 1 0 1
1 0 0 1
1 1 0 1
The two unique rows are 1 1 0 1 and 1 0 0 1.