Skip to content

feat: add solutions to lc problems: No.0807,2170 #3269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 48 additions & 42 deletions solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ gridNew = [ [8, 4, 8, 7],

<!-- solution:start -->

### 方法一
### 方法一:贪心

根据题目描述,我们可以将每个单元格 $(i, j)$ 的值增加至第 $i$ 行的最大值和第 $j$ 列的最大值中的较小值,这样可以保证不影响天际线,即每个单元格增加的高度为 $\min(\text{rowMax}[i], \text{colMax}[j]) - \text{grid}[i][j]$。

因此,我们可以先遍历一次矩阵,分别计算出每行和每列的最大值,记录在数组 $\text{rowMax}$ 和 $\text{colMax}$ 中,然后再遍历一次矩阵,计算出答案即可。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵 $\text{grid}$ 的边长。

<!-- tabs:start -->

Expand All @@ -76,12 +82,12 @@ gridNew = [ [8, 4, 8, 7],
```python
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
rmx = [max(row) for row in grid]
cmx = [max(col) for col in zip(*grid)]
row_max = [max(row) for row in grid]
col_max = [max(col) for col in zip(*grid)]
return sum(
(min(rmx[i], cmx[j]) - grid[i][j])
for i in range(len(grid))
for j in range(len(grid[0]))
min(row_max[i], col_max[j]) - x
for i, row in enumerate(grid)
for j, x in enumerate(row)
)
```

Expand All @@ -91,18 +97,18 @@ class Solution:
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] rmx = new int[m];
int[] cmx = new int[n];
int[] rowMax = new int[m];
int[] colMax = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = Math.max(rmx[i], grid[i][j]);
cmx[j] = Math.max(cmx[j], grid[i][j]);
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
colMax[j] = Math.max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
Expand All @@ -116,19 +122,22 @@ class Solution {
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> rmx(m, 0);
vector<int> cmx(n, 0);
int m = grid.size();
int n = grid[0].size();
vector<int> rowMax(m);
vector<int> colMax(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = max(rmx[i], grid[i][j]);
cmx[j] = max(cmx[j], grid[i][j]);
rowMax[i] = max(rowMax[i], grid[i][j]);
colMax[j] = max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans += min(rmx[i], cmx[j]) - grid[i][j];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
}
};
Expand All @@ -137,45 +146,42 @@ public:
#### Go

```go
func maxIncreaseKeepingSkyline(grid [][]int) int {
m, n := len(grid), len(grid[0])
rmx := make([]int, m)
cmx := make([]int, n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
rmx[i] = max(rmx[i], grid[i][j])
cmx[j] = max(cmx[j], grid[i][j])
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
rowMax := make([]int, len(grid))
colMax := make([]int, len(grid[0]))
for i, row := range grid {
for j, x := range row {
rowMax[i] = max(rowMax[i], x)
colMax[j] = max(colMax[j], x)
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans += min(rmx[i], cmx[j]) - grid[i][j]
for i, row := range grid {
for j, x := range row {
ans += min(rowMax[i], colMax[j]) - x
}
}
return ans
return
}
```

#### TypeScript

```ts
function maxIncreaseKeepingSkyline(grid: number[][]): number {
let rows = grid.map(arr => Math.max(...arr)),
cols = [];
let m = grid.length,
n = grid[0].length;
for (let j = 0; j < n; ++j) {
cols[j] = grid[0][j];
for (let i = 1; i < m; ++i) {
cols[j] = Math.max(cols[j], grid[i][j]);
const m = grid.length;
const n = grid[0].length;
const rowMax = Array(m).fill(0);
const colMax = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
colMax[j] = Math.max(colMax[j], grid[i][j]);
}
}

let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans += Math.min(rows[i], cols[j]) - grid[i][j];
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ gridNew = [ [8, 4, 8, 7],

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

According to the problem description, we can increase the value of each cell $(i, j)$ to the smaller value between the maximum value of the $i$-th row and the $j$-th column, ensuring it does not affect the skyline. Thus, the height added to each cell is $\min(\text{rowMax}[i], \text{colMax}[j]) - \text{grid}[i][j]$.

Therefore, we can first traverse the matrix once to calculate the maximum value of each row and column, storing them in the arrays $\text{rowMax}$ and $\text{colMax}$, respectively. Then, we traverse the matrix again to compute the answer.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the side length of the matrix $\text{grid}$.

<!-- tabs:start -->

Expand All @@ -74,12 +80,12 @@ gridNew = [ [8, 4, 8, 7],
```python
class Solution:
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
rmx = [max(row) for row in grid]
cmx = [max(col) for col in zip(*grid)]
row_max = [max(row) for row in grid]
col_max = [max(col) for col in zip(*grid)]
return sum(
(min(rmx[i], cmx[j]) - grid[i][j])
for i in range(len(grid))
for j in range(len(grid[0]))
min(row_max[i], col_max[j]) - x
for i, row in enumerate(grid)
for j, x in enumerate(row)
)
```

Expand All @@ -89,18 +95,18 @@ class Solution:
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] rmx = new int[m];
int[] cmx = new int[n];
int[] rowMax = new int[m];
int[] colMax = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = Math.max(rmx[i], grid[i][j]);
cmx[j] = Math.max(cmx[j], grid[i][j]);
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
colMax[j] = Math.max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
Expand All @@ -114,19 +120,22 @@ class Solution {
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> rmx(m, 0);
vector<int> cmx(n, 0);
int m = grid.size();
int n = grid[0].size();
vector<int> rowMax(m);
vector<int> colMax(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = max(rmx[i], grid[i][j]);
cmx[j] = max(cmx[j], grid[i][j]);
rowMax[i] = max(rowMax[i], grid[i][j]);
colMax[j] = max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans += min(rmx[i], cmx[j]) - grid[i][j];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
}
};
Expand All @@ -135,45 +144,42 @@ public:
#### Go

```go
func maxIncreaseKeepingSkyline(grid [][]int) int {
m, n := len(grid), len(grid[0])
rmx := make([]int, m)
cmx := make([]int, n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
rmx[i] = max(rmx[i], grid[i][j])
cmx[j] = max(cmx[j], grid[i][j])
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
rowMax := make([]int, len(grid))
colMax := make([]int, len(grid[0]))
for i, row := range grid {
for j, x := range row {
rowMax[i] = max(rowMax[i], x)
colMax[j] = max(colMax[j], x)
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans += min(rmx[i], cmx[j]) - grid[i][j]
for i, row := range grid {
for j, x := range row {
ans += min(rowMax[i], colMax[j]) - x
}
}
return ans
return
}
```

#### TypeScript

```ts
function maxIncreaseKeepingSkyline(grid: number[][]): number {
let rows = grid.map(arr => Math.max(...arr)),
cols = [];
let m = grid.length,
n = grid[0].length;
for (let j = 0; j < n; ++j) {
cols[j] = grid[0][j];
for (let i = 1; i < m; ++i) {
cols[j] = Math.max(cols[j], grid[i][j]);
const m = grid.length;
const n = grid[0].length;
const rowMax = Array(m).fill(0);
const colMax = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
colMax[j] = Math.max(colMax[j], grid[i][j]);
}
}

let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans += Math.min(rows[i], cols[j]) - grid[i][j];
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> rmx(m, 0);
vector<int> cmx(n, 0);
int m = grid.size();
int n = grid[0].size();
vector<int> rowMax(m);
vector<int> colMax(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = max(rmx[i], grid[i][j]);
cmx[j] = max(cmx[j], grid[i][j]);
rowMax[i] = max(rowMax[i], grid[i][j]);
colMax[j] = max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans += min(rmx[i], cmx[j]) - grid[i][j];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
func maxIncreaseKeepingSkyline(grid [][]int) int {
m, n := len(grid), len(grid[0])
rmx := make([]int, m)
cmx := make([]int, n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
rmx[i] = max(rmx[i], grid[i][j])
cmx[j] = max(cmx[j], grid[i][j])
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
rowMax := make([]int, len(grid))
colMax := make([]int, len(grid[0]))
for i, row := range grid {
for j, x := range row {
rowMax[i] = max(rowMax[i], x)
colMax[j] = max(colMax[j], x)
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans += min(rmx[i], cmx[j]) - grid[i][j]
for i, row := range grid {
for j, x := range row {
ans += min(rowMax[i], colMax[j]) - x
}
}
return ans
return
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] rmx = new int[m];
int[] cmx = new int[n];
int[] rowMax = new int[m];
int[] colMax = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rmx[i] = Math.max(rmx[i], grid[i][j]);
cmx[j] = Math.max(cmx[j], grid[i][j]);
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
colMax[j] = Math.max(colMax[j], grid[i][j]);
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
}
}
return ans;
Expand Down
Loading
Loading