Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 94a1e0b

Browse files
add 807
1 parent 0644098 commit 94a1e0b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ _If you like this project, please leave me a star._ ★
187187
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | |Easy| HashMap
188188
|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_814.java) | |Medium| recursion, DFS
189189
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | |Easy| HashMap
190+
|807|[Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_807.java) | |Medium|
190191
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | |Easy|
191192
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | |Easy|
192193
|800|[Similar RGB Color](https://leetcode.com/problems/similar-rgb-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_800.java) | |Easy|
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
5+
/**
6+
* 807. Max Increase to Keep City Skyline
7+
*
8+
* In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there.
9+
* We are allowed to increase the height of any number of buildings,
10+
* by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.
11+
* At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right,
12+
* must be the same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles
13+
* formed by all the buildings when viewed from a distance. See the following example.
14+
* What is the maximum total sum that the height of the buildings can be increased?
15+
*
16+
* Example:
17+
* Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
18+
* Output: 35
19+
* Explanation:
20+
* The grid is:
21+
* [ [3, 0, 8, 4],
22+
* [2, 4, 5, 7],
23+
* [9, 2, 6, 3],
24+
* [0, 3, 1, 0] ]
25+
*
26+
* The skyline viewed from top or bottom is: [9, 4, 8, 7]
27+
* The skyline viewed from left or right is: [8, 7, 9, 3]
28+
* The grid after increasing the height of buildings without affecting skylines is:
29+
* gridNew = [ [8, 4, 8, 7],
30+
* [7, 4, 7, 7],
31+
* [9, 4, 8, 7],
32+
* [3, 3, 3, 3] ]
33+
*
34+
* Notes:
35+
* 1 < grid.length = grid[0].length <= 50.
36+
* All heights grid[i][j] are in the range [0, 100].
37+
* All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.
38+
* */
39+
public class _807 {
40+
public static class Solution1 {
41+
public int maxIncreaseKeepingSkyline(int[][] grid) {
42+
int size = grid.length;
43+
int[] horizontalLimits = new int[size];
44+
int[] verticalLimits = new int[size];
45+
for (int i = 0; i < size; i++) {
46+
int horizontalLimit = grid[i][0];
47+
for (int j = 1; j < size; j++) {
48+
horizontalLimit = Math.max(horizontalLimit, grid[i][j]);
49+
}
50+
horizontalLimits[i] = horizontalLimit;
51+
}
52+
for (int j = 0; j < size; j++) {
53+
int verticalLimit = grid[0][j];
54+
for (int i = 1; i < size; i++) {
55+
verticalLimit = Math.max(verticalLimit, grid[i][j]);
56+
}
57+
verticalLimits[j] = verticalLimit;
58+
}
59+
int increases = 0;
60+
for (int i = 0; i < size; i++) {
61+
for (int j = 0; j < size; j++) {
62+
if (grid[i][j] != horizontalLimits[i] && grid[i][j] != verticalLimits[j]) {
63+
increases += Math.min(horizontalLimits[i], verticalLimits[j]) - grid[i][j];
64+
}
65+
}
66+
}
67+
return increases;
68+
}
69+
}
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._807;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _807Test {
10+
private static _807.Solution1 solution1;
11+
private static int[][] grid;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _807.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
grid = new int[][]{
21+
{3, 0, 8, 4},
22+
{2, 4, 5, 7},
23+
{9, 2, 6, 3},
24+
{0, 3, 1, 0}
25+
};
26+
assertEquals(35, solution1.maxIncreaseKeepingSkyline(grid));
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)