Skip to content

Commit 5378885

Browse files
committed
feat: add solutions to lc problem: No.0807. Max Increase to Keep City
Skyline
1 parent a69bd02 commit 5378885

File tree

6 files changed

+266
-39
lines changed

6 files changed

+266
-39
lines changed

solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,121 @@ gridNew = [ [8, 4, 8, 7],
4242
<li>一座建筑物占据一个<code>grid[i][j]</code>:换言之,它们是 <code>1 x 1 x grid[i][j]</code> 的长方体。</li>
4343
</ul>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->
4948

49+
先求出东西方向 we 每一行的最大值、南北方向 ns 每一列的最大值。
50+
51+
然后遍历二维数组 grid,对于每个元素,能增加的最大高度是 `min(we[i], ns[j]) - grid[i][j]`。累加所有元素能增加的最大高度即可。
52+
5053
<!-- tabs:start -->
5154

5255
### **Python3**
5356

5457
<!-- 这里可写当前语言的特殊实现逻辑 -->
5558

5659
```python
57-
60+
class Solution:
61+
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
62+
m, n = len(grid), len(grid[0])
63+
we = [max(item) for item in grid]
64+
ns = [max([grid[i][j] for i in range(m)]) for j in range(n)]
65+
res = 0
66+
for i in range(m):
67+
for j in range(n):
68+
res += min(we[i], ns[j]) - grid[i][j]
69+
return res
5870
```
5971

6072
### **Java**
6173

6274
<!-- 这里可写当前语言的特殊实现逻辑 -->
6375

6476
```java
77+
class Solution {
78+
public int maxIncreaseKeepingSkyline(int[][] grid) {
79+
int m = grid.length, n = grid[0].length;
80+
int[] we = new int[m];
81+
int[] ns = new int[n];
82+
for (int i = 0; i < m; ++i) {
83+
for (int j = 0; j < n; ++j) {
84+
we[i] = Math.max(we[i], grid[i][j]);
85+
ns[j] = Math.max(ns[j], grid[i][j]);
86+
}
87+
}
88+
int res = 0;
89+
for (int i = 0; i < m; ++i) {
90+
for (int j = 0; j < n; ++j) {
91+
res += Math.min(we[i], ns[j]) - grid[i][j];
92+
}
93+
}
94+
return res;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
105+
int m = grid.size(), n = grid[0].size();
106+
vector<int> we(m, 0);
107+
vector<int> ns(n, 0);
108+
for (int i = 0; i < m; ++i) {
109+
for (int j = 0; j < n; ++j) {
110+
we[i] = max(we[i], grid[i][j]);
111+
ns[j] = max(ns[j], grid[i][j]);
112+
}
113+
}
114+
int res = 0;
115+
for (int i = 0; i < m; ++i) {
116+
for (int j = 0; j < n; ++j) {
117+
res += min(we[i], ns[j]) - grid[i][j];
118+
}
119+
}
120+
return res;
121+
}
122+
};
123+
```
65124
125+
### **Go**
126+
127+
```go
128+
func maxIncreaseKeepingSkyline(grid [][]int) int {
129+
m, n := len(grid), len(grid[0])
130+
we := make([]int, m)
131+
ns := make([]int, n)
132+
for i := 0; i < m; i++ {
133+
for j := 0; j < n; j++ {
134+
we[i] = max(we[i], grid[i][j])
135+
ns[j] = max(ns[j], grid[i][j])
136+
}
137+
}
138+
res := 0
139+
for i := 0; i < m; i++ {
140+
for j := 0; j < n; j++ {
141+
res += min(we[i], ns[j]) - grid[i][j]
142+
}
143+
}
144+
return res
145+
}
146+
147+
func max(a, b int) int {
148+
if a > b {
149+
return a
150+
}
151+
return b
152+
}
153+
154+
func min(a, b int) int {
155+
if a < b {
156+
return a
157+
}
158+
return b
159+
}
66160
```
67161

68162
### **...**

solution/0800-0899/0807.Max Increase to Keep City Skyline/README_EN.md

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66

77
<p>In a 2 dimensional array <code>grid</code>, each value <code>grid[i][j]</code> represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts&nbsp;can be different for different buildings). Height&nbsp;0 is considered to be a&nbsp;building&nbsp;as well.&nbsp;</p>
88

9-
10-
119
<p>At the end, the &quot;skyline&quot; when viewed from all four directions&nbsp;of the grid, i.e.&nbsp;top, bottom, left, and right,&nbsp;must be the same as the&nbsp;skyline of the original grid. A city&#39;s skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See&nbsp;the following example.</p>
1210

13-
14-
1511
<p>What is the maximum total sum that the height of the buildings can be increased?</p>
1612

17-
18-
1913
<pre>
2014

2115
<strong>Example:</strong>
@@ -60,34 +54,119 @@ gridNew = [ [8, 4, 8, 7],
6054

6155
</pre>
6256

63-
64-
6557
<p><strong>Notes: </strong></p>
6658

67-
68-
6959
<ul>
7060
<li><code>1 &lt; grid.length = grid[0].length &lt;= 50</code>.</li>
7161
<li>All heights <code>grid[i][j]</code> are in the range <code>[0, 100]</code>.</li>
7262
<li>All buildings in <code>grid[i][j]</code> occupy the entire grid cell: that is, they are a <code>1 x 1 x grid[i][j]</code> rectangular prism.</li>
7363
</ul>
7464

75-
76-
7765
## Solutions
7866

7967
<!-- tabs:start -->
8068

8169
### **Python3**
8270

8371
```python
84-
72+
class Solution:
73+
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
74+
m, n = len(grid), len(grid[0])
75+
we = [max(item) for item in grid]
76+
ns = [max([grid[i][j] for i in range(m)]) for j in range(n)]
77+
res = 0
78+
for i in range(m):
79+
for j in range(n):
80+
res += min(we[i], ns[j]) - grid[i][j]
81+
return res
8582
```
8683

8784
### **Java**
8885

8986
```java
87+
class Solution {
88+
public int maxIncreaseKeepingSkyline(int[][] grid) {
89+
int m = grid.length, n = grid[0].length;
90+
int[] we = new int[m];
91+
int[] ns = new int[n];
92+
for (int i = 0; i < m; ++i) {
93+
for (int j = 0; j < n; ++j) {
94+
we[i] = Math.max(we[i], grid[i][j]);
95+
ns[j] = Math.max(ns[j], grid[i][j]);
96+
}
97+
}
98+
int res = 0;
99+
for (int i = 0; i < m; ++i) {
100+
for (int j = 0; j < n; ++j) {
101+
res += Math.min(we[i], ns[j]) - grid[i][j];
102+
}
103+
}
104+
return res;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
115+
int m = grid.size(), n = grid[0].size();
116+
vector<int> we(m, 0);
117+
vector<int> ns(n, 0);
118+
for (int i = 0; i < m; ++i) {
119+
for (int j = 0; j < n; ++j) {
120+
we[i] = max(we[i], grid[i][j]);
121+
ns[j] = max(ns[j], grid[i][j]);
122+
}
123+
}
124+
int res = 0;
125+
for (int i = 0; i < m; ++i) {
126+
for (int j = 0; j < n; ++j) {
127+
res += min(we[i], ns[j]) - grid[i][j];
128+
}
129+
}
130+
return res;
131+
}
132+
};
133+
```
90134
135+
### **Go**
136+
137+
```go
138+
func maxIncreaseKeepingSkyline(grid [][]int) int {
139+
m, n := len(grid), len(grid[0])
140+
we := make([]int, m)
141+
ns := make([]int, n)
142+
for i := 0; i < m; i++ {
143+
for j := 0; j < n; j++ {
144+
we[i] = max(we[i], grid[i][j])
145+
ns[j] = max(ns[j], grid[i][j])
146+
}
147+
}
148+
res := 0
149+
for i := 0; i < m; i++ {
150+
for j := 0; j < n; j++ {
151+
res += min(we[i], ns[j]) - grid[i][j]
152+
}
153+
}
154+
return res
155+
}
156+
157+
func max(a, b int) int {
158+
if a > b {
159+
return a
160+
}
161+
return b
162+
}
163+
164+
func min(a, b int) int {
165+
if a < b {
166+
return a
167+
}
168+
return b
169+
}
91170
```
92171

93172
### **...**
Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
class Solution {
22
public:
3-
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid)
4-
{
5-
6-
vector<int> h(grid.size(), -1), w(grid[0]) ;
7-
8-
for (int i = 0; i < grid.size(); ++i)
9-
for (int j = 0; j < grid[i].size(); ++j)
10-
{
11-
if (grid[i][j] > h[i])
12-
h[i] = grid[i][j] ;
13-
if (grid[i][j] > w[j])
14-
w[j] = grid[i][j] ;
3+
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<int> we(m, 0);
6+
vector<int> ns(n, 0);
7+
for (int i = 0; i < m; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
we[i] = max(we[i], grid[i][j]);
10+
ns[j] = max(ns[j], grid[i][j]);
1511
}
16-
int sum = 0 ;
17-
18-
for (int i = 0; i < grid.size(); ++i)
19-
for (int j = 0; j < grid.size(); ++j)
20-
{
21-
int m = h[i] < w[j]? h[i]: w[j] ;
22-
if (grid[i][j] < m)
23-
sum += m - grid[i][j] ;
12+
}
13+
int res = 0;
14+
for (int i = 0; i < m; ++i) {
15+
for (int j = 0; j < n; ++j) {
16+
res += min(we[i], ns[j]) - grid[i][j];
2417
}
25-
26-
27-
return sum ;
18+
}
19+
return res;
2820
}
29-
};
21+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func maxIncreaseKeepingSkyline(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
we := make([]int, m)
4+
ns := make([]int, n)
5+
for i := 0; i < m; i++ {
6+
for j := 0; j < n; j++ {
7+
we[i] = max(we[i], grid[i][j])
8+
ns[j] = max(ns[j], grid[i][j])
9+
}
10+
}
11+
res := 0
12+
for i := 0; i < m; i++ {
13+
for j := 0; j < n; j++ {
14+
res += min(we[i], ns[j]) - grid[i][j]
15+
}
16+
}
17+
return res
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
26+
27+
func min(a, b int) int {
28+
if a < b {
29+
return a
30+
}
31+
return b
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxIncreaseKeepingSkyline(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
int[] we = new int[m];
5+
int[] ns = new int[n];
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
we[i] = Math.max(we[i], grid[i][j]);
9+
ns[j] = Math.max(ns[j], grid[i][j]);
10+
}
11+
}
12+
int res = 0;
13+
for (int i = 0; i < m; ++i) {
14+
for (int j = 0; j < n; ++j) {
15+
res += Math.min(we[i], ns[j]) - grid[i][j];
16+
}
17+
}
18+
return res;
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
we = [max(item) for item in grid]
5+
ns = [max([grid[i][j] for i in range(m)]) for j in range(n)]
6+
res = 0
7+
for i in range(m):
8+
for j in range(n):
9+
res += min(we[i], ns[j]) - grid[i][j]
10+
return res

0 commit comments

Comments
 (0)