Skip to content

Commit 46122ba

Browse files
committed
Add solution #807
1 parent c610f2a commit 46122ba

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,391 LeetCode solutions in JavaScript
1+
# 1,392 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -620,6 +620,7 @@
620620
804|[Unique Morse Code Words](./solutions/0804-unique-morse-code-words.js)|Easy|
621621
805|[Split Array With Same Average](./solutions/0805-split-array-with-same-average.js)|Hard|
622622
806|[Number of Lines To Write String](./solutions/0806-number-of-lines-to-write-string.js)|Easy|
623+
807|[Max Increase to Keep City Skyline](./solutions/0807-max-increase-to-keep-city-skyline.js)|Medium|
623624
808|[Soup Servings](./solutions/0808-soup-servings.js)|Medium|
624625
809|[Expressive Words](./solutions/0809-expressive-words.js)|Medium|
625626
810|[Chalkboard XOR Game](./solutions/0810-chalkboard-xor-game.js)|Hard|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 807. Max Increase to Keep City Skyline
3+
* https://leetcode.com/problems/max-increase-to-keep-city-skyline/
4+
* Difficulty: Medium
5+
*
6+
* There is a city composed of n x n blocks, where each block contains a single building shaped
7+
* like a vertical square prism. You are given a 0-indexed n x n integer matrix grid where
8+
* grid[r][c] represents the height of the building located in the block at row r and column c.
9+
*
10+
* A city's skyline is the outer contour formed by all the building when viewing the side of the
11+
* city from a distance. The skyline from each cardinal direction north, east, south, and west
12+
* may be different.
13+
*
14+
* We are allowed to increase the height of any number of buildings by any amount (the amount can
15+
* be different per building). The height of a 0-height building can also be increased. However,
16+
* increasing the height of a building should not affect the city's skyline from any cardinal
17+
* direction.
18+
*
19+
* Return the maximum total sum that the height of the buildings can be increased by without
20+
* changing the city's skyline from any cardinal direction.
21+
*/
22+
23+
/**
24+
* @param {number[][]} grid
25+
* @return {number}
26+
*/
27+
var maxIncreaseKeepingSkyline = function(grid) {
28+
let result = 0;
29+
const rowMaxes = grid.map(row => Math.max(...row));
30+
const columnMaxes = grid.map((row, index) => grid.map(row => row[index]))
31+
.map(row => Math.max(...row));
32+
33+
for (let i = 0; i < grid.length; i++) {
34+
for (let j = 0; j < grid[i].length; j++) {
35+
const currentHeight = grid[i][j];
36+
const minHeight = Math.min(
37+
Math.max(rowMaxes[i], currentHeight),
38+
Math.max(columnMaxes[j], currentHeight)
39+
);
40+
41+
result += (minHeight - currentHeight);
42+
}
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)