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.
classSolution { public: vector<vector<int>> highestPeak(vector<vector<int>>& A) { int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1}; int n = A.size(), m = A[0].size();
queue<pair<int,int>> q; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(A[i][j] == -1) continue; for(int k = 0; k < 4; k++) { int ny = i + dy[k], nx = j + dx[k]; if(0 <= ny and ny < n and0 <= nx and nx < m and A[ny][nx] == -1) q.push({ny,nx}); } } } while(!q.empty()) { queue<pair<int, int>> qq; while(!q.empty()) { auto [y, x] = q.front(); q.pop(); int mi = INT_MAX; for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and0 <= nx and nx < m and A[ny][nx] >= 0) { mi = min(mi, A[ny][nx]); } } A[y][x] = mi + 1; qq.push({y, x}); } while(!qq.empty()) { auto [y, x] = qq.front(); qq.pop(); for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and0 <= nx and nx < m and A[ny][nx] == -1) { q.push({ny, nx}); A[ny][nx] = -2; } } } } return A; } };