-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxIncreaseToKeepCitySkyline.java
55 lines (46 loc) · 1.45 KB
/
MaxIncreaseToKeepCitySkyline.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package me.rainking;
/**
* @author Rain
* @description 807.求每行每列的最大值,然后确定每个位置行列最大值中的最小值。
* 如:grid[0][0],
* @date 2018/5/5
*/
public class MaxIncreaseToKeepCitySkyline {
public static void main(String[] args) {
int[][] grid = {
{3, 0, 8, 4},
{2, 4, 5, 7},
{9, 2, 6, 3},
{0, 3, 1, 0}
};
System.out.println(maxIncreaseKeepingSkyline(grid));
}
public static int maxIncreaseKeepingSkyline(int[][] grid) {
int height = grid.length;
int width = grid[0].length;
int[] rowMax = new int[height];
int[] colMax = new int[width];
int oldSum = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//获取数组总和
oldSum += grid[i][j];
//获取行最大值
if (rowMax[i] < grid[i][j]) {
rowMax[i] = grid[i][j];
}
//获取列最大值
if (colMax[j] < grid[i][j]) {
colMax[j] = grid[i][j];
}
}
}
int newSum = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
newSum += (rowMax[i] < colMax[j] ? rowMax[i] : colMax[j]);
}
}
return newSum - oldSum;
}
}