1765. Map of Highest Peak | leetcode daily challenge | shashcode | java | dsa | shashwat | hindi

preview_player
Показать описание
Prerequisite (similar pattern) - rotting oranges:

Problem Link:

Problem Statement:
You are given an integer matrix isWater of size m x n that represents a map of land and water cells.

If isWater[i][j] == 0, cell (i, j) is a land cell.
If isWater[i][j] == 1, cell (i, j) is a water cell.
You must assign each cell a height in a way that follows these rules:

The height of each cell must be non-negative.
If the cell is a water cell, its height must be 0.
Any two adjacent cells must have an absolute height difference of at most 1. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).
Find an assignment of heights such that the maximum height in the matrix is maximized.

Return an integer matrix height of size m x n where height[i][j] is cell (i, j)'s height. If there are multiple solutions, return any of them.

Solution Link:

Custom Comparator:

Lambda Expression:

Dynamic Programming:

Graph Playlist:

Java Plus DSA Placement Course Playlist:

Java Plus DSA Sheet:

Notes:

Telegram Link:

Ultimate Recursion Series Playlist:

Samsung Interview Experience:

Company Tags:
Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe | Samsung

Timestamp:
0:00 - Introduction
Рекомендации по теме
Комментарии
Автор

like target for this video is 150. please do like if you have understood the explanation as well as the code.

shashwat_tiwari_st
Автор

Thanks..Quite similar to Rotten Oranges

DarkDragon-bzqp
Автор

C++ sol
class Solution {
public:
vector<vector<int>> isWater) {
int n=isWater.size();
int m=isWater[0].size();

vector<vector<int>> vis(n, vector<int>(m, 0));
vector<vector<int>> ans(n, vector<int>(m));

queue<pair<int, pair<int, int>>> q;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(isWater[i][j]==1){
q.push({0, {i, j}});
vis[i][j]=1;

}
}
}
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};

while(!q.empty()){
auto it=q.front();
q.pop();
int step=it.first;
int r=it.second.first;
int c=it.second.second;

ans[r][c]=step;


for(int k=0;k<4;k++){
int nr=r+dx[k];
int nc=c+dy[k];

if(nr>=0 && nr<n && nc>=0 && nc<m && !vis[nr][nc]){
vis[nr][nc]=1;
q.push({step+1, {nr, nc}});
}
}
}
return ans;

}
};

darelixoo
welcome to shbcf.ru