|
| 1 | +// Source : https://leetcode.com/problems/map-of-highest-peak/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-26 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given an integer matrix isWater of size m x n that represents a map of land and water cells. |
| 8 | + * |
| 9 | + * If isWater[i][j] == 0, cell (i, j) is a land cell. |
| 10 | + * If isWater[i][j] == 1, cell (i, j) is a water cell. |
| 11 | + * |
| 12 | + * You must assign each cell a height in a way that follows these rules: |
| 13 | + * |
| 14 | + * The height of each cell must be non-negative. |
| 15 | + * If the cell is a water cell, its height must be 0. |
| 16 | + * Any two adjacent cells must have an absolute height difference of at most 1. A cell is |
| 17 | + * adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., |
| 18 | + * their sides are touching). |
| 19 | + * |
| 20 | + * Find an assignment of heights such that the maximum height in the matrix is maximized. |
| 21 | + * |
| 22 | + * Return an integer matrix height of size m x n where height[i][j] is cell (i, j)'s height. If there |
| 23 | + * are multiple solutions, return any of them. |
| 24 | + * |
| 25 | + * Example 1: |
| 26 | + * |
| 27 | + * Input: isWater = [[0,1],[0,0]] |
| 28 | + * Output: [[1,0],[2,1]] |
| 29 | + * Explanation: The image shows the assigned heights of each cell. |
| 30 | + * The blue cell is the water cell, and the green cells are the land cells. |
| 31 | + * |
| 32 | + * Example 2: |
| 33 | + * |
| 34 | + * Input: isWater = [[0,0,1],[1,0,0],[0,0,0]] |
| 35 | + * Output: [[1,1,0],[0,1,1],[1,2,2]] |
| 36 | + * Explanation: A height of 2 is the maximum possible height of any assignment. |
| 37 | + * Any height assignment that has a maximum height of 2 while still meeting the rules will also be |
| 38 | + * accepted. |
| 39 | + * |
| 40 | + * Constraints: |
| 41 | + * |
| 42 | + * m == isWater.length |
| 43 | + * n == isWater[i].length |
| 44 | + * 1 <= m, n <= 1000 |
| 45 | + * isWater[i][j] is 0 or 1. |
| 46 | + * There is at least one water cell. |
| 47 | + ******************************************************************************************************/ |
| 48 | + |
| 49 | +class Cell{ |
| 50 | +public: |
| 51 | + int x; |
| 52 | + int y; |
| 53 | + int height; |
| 54 | +}; |
| 55 | + |
| 56 | +class Solution { |
| 57 | +private: |
| 58 | + void setHeight(vector<vector<int>>& height, |
| 59 | + queue<Cell>& q, |
| 60 | + int x, int y, int h, |
| 61 | + int m, int n) |
| 62 | + { |
| 63 | + if (x < 0 || y < 0 || x>=m || y>=n ) return; |
| 64 | + if (height[x][y] == -1) { |
| 65 | + height[x][y] = h; |
| 66 | + q.push({x, y, h}); |
| 67 | + } |
| 68 | + } |
| 69 | +public: |
| 70 | + vector<vector<int>> highestPeak(vector<vector<int>>& isWater) { |
| 71 | + |
| 72 | + int m = isWater.size(); |
| 73 | + int n = isWater[0].size(); |
| 74 | + |
| 75 | + vector<vector<int>> height(m, vector(n, -1)); |
| 76 | + queue<Cell> q; |
| 77 | + |
| 78 | + for (int i=0; i<m; i++) { |
| 79 | + for (int j=0; j<n; j++) { |
| 80 | + if (isWater[i][j]) { |
| 81 | + height[i][j] = 0; |
| 82 | + q.push({i, j, 0}); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + while(!q.empty()){ |
| 88 | + auto cell = q.front(); q.pop(); |
| 89 | + setHeight(height, q, cell.x-1, cell.y, cell.height+1, m, n); |
| 90 | + setHeight(height, q, cell.x+1, cell.y, cell.height+1, m, n); |
| 91 | + setHeight(height, q, cell.x, cell.y-1, cell.height+1, m, n); |
| 92 | + setHeight(height, q, cell.x, cell.y+1, cell.height+1, m, n); |
| 93 | + } |
| 94 | + |
| 95 | + return height; |
| 96 | + } |
| 97 | + |
| 98 | +}; |
0 commit comments