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

Commit 772a22d

Browse files
authored
feat: add solutions to lc problems: No.0807,2170 (doocs#3269)
* No.0807.Max Increase to Keep City Skyline * No.2170.Minimum Operations to Make the Array Alternating
1 parent 7f8adac commit 772a22d

File tree

14 files changed

+443
-454
lines changed

14 files changed

+443
-454
lines changed

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

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ gridNew = [ [8, 4, 8, 7],
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:贪心
71+
72+
根据题目描述,我们可以将每个单元格 $(i, j)$ 的值增加至第 $i$ 行的最大值和第 $j$ 列的最大值中的较小值,这样可以保证不影响天际线,即每个单元格增加的高度为 $\min(\text{rowMax}[i], \text{colMax}[j]) - \text{grid}[i][j]$。
73+
74+
因此,我们可以先遍历一次矩阵,分别计算出每行和每列的最大值,记录在数组 $\text{rowMax}$ 和 $\text{colMax}$ 中,然后再遍历一次矩阵,计算出答案即可。
75+
76+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵 $\text{grid}$ 的边长。
7177

7278
<!-- tabs:start -->
7379

@@ -76,12 +82,12 @@ gridNew = [ [8, 4, 8, 7],
7682
```python
7783
class Solution:
7884
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
79-
rmx = [max(row) for row in grid]
80-
cmx = [max(col) for col in zip(*grid)]
85+
row_max = [max(row) for row in grid]
86+
col_max = [max(col) for col in zip(*grid)]
8187
return sum(
82-
(min(rmx[i], cmx[j]) - grid[i][j])
83-
for i in range(len(grid))
84-
for j in range(len(grid[0]))
88+
min(row_max[i], col_max[j]) - x
89+
for i, row in enumerate(grid)
90+
for j, x in enumerate(row)
8591
)
8692
```
8793

@@ -91,18 +97,18 @@ class Solution:
9197
class Solution {
9298
public int maxIncreaseKeepingSkyline(int[][] grid) {
9399
int m = grid.length, n = grid[0].length;
94-
int[] rmx = new int[m];
95-
int[] cmx = new int[n];
100+
int[] rowMax = new int[m];
101+
int[] colMax = new int[n];
96102
for (int i = 0; i < m; ++i) {
97103
for (int j = 0; j < n; ++j) {
98-
rmx[i] = Math.max(rmx[i], grid[i][j]);
99-
cmx[j] = Math.max(cmx[j], grid[i][j]);
104+
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
105+
colMax[j] = Math.max(colMax[j], grid[i][j]);
100106
}
101107
}
102108
int ans = 0;
103109
for (int i = 0; i < m; ++i) {
104110
for (int j = 0; j < n; ++j) {
105-
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
111+
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
106112
}
107113
}
108114
return ans;
@@ -116,19 +122,22 @@ class Solution {
116122
class Solution {
117123
public:
118124
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
119-
int m = grid.size(), n = grid[0].size();
120-
vector<int> rmx(m, 0);
121-
vector<int> cmx(n, 0);
125+
int m = grid.size();
126+
int n = grid[0].size();
127+
vector<int> rowMax(m);
128+
vector<int> colMax(n);
122129
for (int i = 0; i < m; ++i) {
123130
for (int j = 0; j < n; ++j) {
124-
rmx[i] = max(rmx[i], grid[i][j]);
125-
cmx[j] = max(cmx[j], grid[i][j]);
131+
rowMax[i] = max(rowMax[i], grid[i][j]);
132+
colMax[j] = max(colMax[j], grid[i][j]);
126133
}
127134
}
128135
int ans = 0;
129-
for (int i = 0; i < m; ++i)
130-
for (int j = 0; j < n; ++j)
131-
ans += min(rmx[i], cmx[j]) - grid[i][j];
136+
for (int i = 0; i < m; ++i) {
137+
for (int j = 0; j < n; ++j) {
138+
ans += min(rowMax[i], colMax[j]) - grid[i][j];
139+
}
140+
}
132141
return ans;
133142
}
134143
};
@@ -137,45 +146,42 @@ public:
137146
#### Go
138147
139148
```go
140-
func maxIncreaseKeepingSkyline(grid [][]int) int {
141-
m, n := len(grid), len(grid[0])
142-
rmx := make([]int, m)
143-
cmx := make([]int, n)
144-
for i := 0; i < m; i++ {
145-
for j := 0; j < n; j++ {
146-
rmx[i] = max(rmx[i], grid[i][j])
147-
cmx[j] = max(cmx[j], grid[i][j])
149+
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
150+
rowMax := make([]int, len(grid))
151+
colMax := make([]int, len(grid[0]))
152+
for i, row := range grid {
153+
for j, x := range row {
154+
rowMax[i] = max(rowMax[i], x)
155+
colMax[j] = max(colMax[j], x)
148156
}
149157
}
150-
ans := 0
151-
for i := 0; i < m; i++ {
152-
for j := 0; j < n; j++ {
153-
ans += min(rmx[i], cmx[j]) - grid[i][j]
158+
for i, row := range grid {
159+
for j, x := range row {
160+
ans += min(rowMax[i], colMax[j]) - x
154161
}
155162
}
156-
return ans
163+
return
157164
}
158165
```
159166

160167
#### TypeScript
161168

162169
```ts
163170
function maxIncreaseKeepingSkyline(grid: number[][]): number {
164-
let rows = grid.map(arr => Math.max(...arr)),
165-
cols = [];
166-
let m = grid.length,
167-
n = grid[0].length;
168-
for (let j = 0; j < n; ++j) {
169-
cols[j] = grid[0][j];
170-
for (let i = 1; i < m; ++i) {
171-
cols[j] = Math.max(cols[j], grid[i][j]);
171+
const m = grid.length;
172+
const n = grid[0].length;
173+
const rowMax = Array(m).fill(0);
174+
const colMax = Array(n).fill(0);
175+
for (let i = 0; i < m; ++i) {
176+
for (let j = 0; j < n; ++j) {
177+
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
178+
colMax[j] = Math.max(colMax[j], grid[i][j]);
172179
}
173180
}
174-
175181
let ans = 0;
176182
for (let i = 0; i < m; ++i) {
177183
for (let j = 0; j < n; ++j) {
178-
ans += Math.min(rows[i], cols[j]) - grid[i][j];
184+
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
179185
}
180186
}
181187
return ans;

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

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ gridNew = [ [8, 4, 8, 7],
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Greedy
69+
70+
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]$.
71+
72+
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.
73+
74+
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}$.
6975

7076
<!-- tabs:start -->
7177

@@ -74,12 +80,12 @@ gridNew = [ [8, 4, 8, 7],
7480
```python
7581
class Solution:
7682
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
77-
rmx = [max(row) for row in grid]
78-
cmx = [max(col) for col in zip(*grid)]
83+
row_max = [max(row) for row in grid]
84+
col_max = [max(col) for col in zip(*grid)]
7985
return sum(
80-
(min(rmx[i], cmx[j]) - grid[i][j])
81-
for i in range(len(grid))
82-
for j in range(len(grid[0]))
86+
min(row_max[i], col_max[j]) - x
87+
for i, row in enumerate(grid)
88+
for j, x in enumerate(row)
8389
)
8490
```
8591

@@ -89,18 +95,18 @@ class Solution:
8995
class Solution {
9096
public int maxIncreaseKeepingSkyline(int[][] grid) {
9197
int m = grid.length, n = grid[0].length;
92-
int[] rmx = new int[m];
93-
int[] cmx = new int[n];
98+
int[] rowMax = new int[m];
99+
int[] colMax = new int[n];
94100
for (int i = 0; i < m; ++i) {
95101
for (int j = 0; j < n; ++j) {
96-
rmx[i] = Math.max(rmx[i], grid[i][j]);
97-
cmx[j] = Math.max(cmx[j], grid[i][j]);
102+
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
103+
colMax[j] = Math.max(colMax[j], grid[i][j]);
98104
}
99105
}
100106
int ans = 0;
101107
for (int i = 0; i < m; ++i) {
102108
for (int j = 0; j < n; ++j) {
103-
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
109+
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
104110
}
105111
}
106112
return ans;
@@ -114,19 +120,22 @@ class Solution {
114120
class Solution {
115121
public:
116122
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
117-
int m = grid.size(), n = grid[0].size();
118-
vector<int> rmx(m, 0);
119-
vector<int> cmx(n, 0);
123+
int m = grid.size();
124+
int n = grid[0].size();
125+
vector<int> rowMax(m);
126+
vector<int> colMax(n);
120127
for (int i = 0; i < m; ++i) {
121128
for (int j = 0; j < n; ++j) {
122-
rmx[i] = max(rmx[i], grid[i][j]);
123-
cmx[j] = max(cmx[j], grid[i][j]);
129+
rowMax[i] = max(rowMax[i], grid[i][j]);
130+
colMax[j] = max(colMax[j], grid[i][j]);
124131
}
125132
}
126133
int ans = 0;
127-
for (int i = 0; i < m; ++i)
128-
for (int j = 0; j < n; ++j)
129-
ans += min(rmx[i], cmx[j]) - grid[i][j];
134+
for (int i = 0; i < m; ++i) {
135+
for (int j = 0; j < n; ++j) {
136+
ans += min(rowMax[i], colMax[j]) - grid[i][j];
137+
}
138+
}
130139
return ans;
131140
}
132141
};
@@ -135,45 +144,42 @@ public:
135144
#### Go
136145
137146
```go
138-
func maxIncreaseKeepingSkyline(grid [][]int) int {
139-
m, n := len(grid), len(grid[0])
140-
rmx := make([]int, m)
141-
cmx := make([]int, n)
142-
for i := 0; i < m; i++ {
143-
for j := 0; j < n; j++ {
144-
rmx[i] = max(rmx[i], grid[i][j])
145-
cmx[j] = max(cmx[j], grid[i][j])
147+
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
148+
rowMax := make([]int, len(grid))
149+
colMax := make([]int, len(grid[0]))
150+
for i, row := range grid {
151+
for j, x := range row {
152+
rowMax[i] = max(rowMax[i], x)
153+
colMax[j] = max(colMax[j], x)
146154
}
147155
}
148-
ans := 0
149-
for i := 0; i < m; i++ {
150-
for j := 0; j < n; j++ {
151-
ans += min(rmx[i], cmx[j]) - grid[i][j]
156+
for i, row := range grid {
157+
for j, x := range row {
158+
ans += min(rowMax[i], colMax[j]) - x
152159
}
153160
}
154-
return ans
161+
return
155162
}
156163
```
157164

158165
#### TypeScript
159166

160167
```ts
161168
function maxIncreaseKeepingSkyline(grid: number[][]): number {
162-
let rows = grid.map(arr => Math.max(...arr)),
163-
cols = [];
164-
let m = grid.length,
165-
n = grid[0].length;
166-
for (let j = 0; j < n; ++j) {
167-
cols[j] = grid[0][j];
168-
for (let i = 1; i < m; ++i) {
169-
cols[j] = Math.max(cols[j], grid[i][j]);
169+
const m = grid.length;
170+
const n = grid[0].length;
171+
const rowMax = Array(m).fill(0);
172+
const colMax = Array(n).fill(0);
173+
for (let i = 0; i < m; ++i) {
174+
for (let j = 0; j < n; ++j) {
175+
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
176+
colMax[j] = Math.max(colMax[j], grid[i][j]);
170177
}
171178
}
172-
173179
let ans = 0;
174180
for (let i = 0; i < m; ++i) {
175181
for (let j = 0; j < n; ++j) {
176-
ans += Math.min(rows[i], cols[j]) - grid[i][j];
182+
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
177183
}
178184
}
179185
return ans;
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
class Solution {
22
public:
33
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
4-
int m = grid.size(), n = grid[0].size();
5-
vector<int> rmx(m, 0);
6-
vector<int> cmx(n, 0);
4+
int m = grid.size();
5+
int n = grid[0].size();
6+
vector<int> rowMax(m);
7+
vector<int> colMax(n);
78
for (int i = 0; i < m; ++i) {
89
for (int j = 0; j < n; ++j) {
9-
rmx[i] = max(rmx[i], grid[i][j]);
10-
cmx[j] = max(cmx[j], grid[i][j]);
10+
rowMax[i] = max(rowMax[i], grid[i][j]);
11+
colMax[j] = max(colMax[j], grid[i][j]);
1112
}
1213
}
1314
int ans = 0;
14-
for (int i = 0; i < m; ++i)
15-
for (int j = 0; j < n; ++j)
16-
ans += min(rmx[i], cmx[j]) - grid[i][j];
15+
for (int i = 0; i < m; ++i) {
16+
for (int j = 0; j < n; ++j) {
17+
ans += min(rowMax[i], colMax[j]) - grid[i][j];
18+
}
19+
}
1720
return ans;
1821
}
1922
};
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
func maxIncreaseKeepingSkyline(grid [][]int) int {
2-
m, n := len(grid), len(grid[0])
3-
rmx := make([]int, m)
4-
cmx := make([]int, n)
5-
for i := 0; i < m; i++ {
6-
for j := 0; j < n; j++ {
7-
rmx[i] = max(rmx[i], grid[i][j])
8-
cmx[j] = max(cmx[j], grid[i][j])
1+
func maxIncreaseKeepingSkyline(grid [][]int) (ans int) {
2+
rowMax := make([]int, len(grid))
3+
colMax := make([]int, len(grid[0]))
4+
for i, row := range grid {
5+
for j, x := range row {
6+
rowMax[i] = max(rowMax[i], x)
7+
colMax[j] = max(colMax[j], x)
98
}
109
}
11-
ans := 0
12-
for i := 0; i < m; i++ {
13-
for j := 0; j < n; j++ {
14-
ans += min(rmx[i], cmx[j]) - grid[i][j]
10+
for i, row := range grid {
11+
for j, x := range row {
12+
ans += min(rowMax[i], colMax[j]) - x
1513
}
1614
}
17-
return ans
15+
return
1816
}

solution/0800-0899/0807.Max Increase to Keep City Skyline/Solution.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
class Solution {
22
public int maxIncreaseKeepingSkyline(int[][] grid) {
33
int m = grid.length, n = grid[0].length;
4-
int[] rmx = new int[m];
5-
int[] cmx = new int[n];
4+
int[] rowMax = new int[m];
5+
int[] colMax = new int[n];
66
for (int i = 0; i < m; ++i) {
77
for (int j = 0; j < n; ++j) {
8-
rmx[i] = Math.max(rmx[i], grid[i][j]);
9-
cmx[j] = Math.max(cmx[j], grid[i][j]);
8+
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
9+
colMax[j] = Math.max(colMax[j], grid[i][j]);
1010
}
1111
}
1212
int ans = 0;
1313
for (int i = 0; i < m; ++i) {
1414
for (int j = 0; j < n; ++j) {
15-
ans += Math.min(rmx[i], cmx[j]) - grid[i][j];
15+
ans += Math.min(rowMax[i], colMax[j]) - grid[i][j];
1616
}
1717
}
1818
return ans;

0 commit comments

Comments
 (0)