Skip to content

Commit ff70235

Browse files
committed
New Problem Solution -"Map of Highest Peak"
1 parent ed3c202 commit ff70235

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ LeetCode
3434
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
3535
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C++](./algorithms/cpp/minimumNumberOfOperationsToMoveAllBallsToEachBox/MinimumNumberOfOperationsToMoveAllBallsToEachBox.cpp)|Medium|
3636
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [C++](./algorithms/cpp/mergeStringsAlternately/MergeStringsAlternately.cpp)|Easy|
37+
|1765|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/) | [C++](./algorithms/cpp/mapOfHighestPeak/MapOfHighestPeak.cpp)|Medium|
3738
|1763|[Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [C++](./algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp)|Easy|
3839
|1761|[Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/) | [C++](./algorithms/cpp/minimumDegreeOfAConnectedTrioInAGraph/MinimumDegreeOfAConnectedTrioInAGraph.cpp)|Hard|
3940
|1760|[Minimum Limit of Balls in a Bag](https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/) | [C++](./algorithms/cpp/minimumLimitOfBallsInABag/MinimumLimitOfBallsInABag.cpp)|Medium|
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)