-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMaxArea.java
51 lines (46 loc) · 1.25 KB
/
MaxArea.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
public class MaxArea {
public int maxArea(int[] height) {
/*穷举是所有算法的核心*/
if (height == null || height.length ==0){
return 0;
}
int m = height.length;
int i = 0;
int j = m-1;
int res = 0;
while (i<j){
if (height[i]<height[j]){
int temp = height[i]*(j-i);
res = Math.max(res,temp);
i++;
}else {
int temp = height[j]*(j-i);
res = Math.max(res,temp);
j--;
}
}
return res;
}
/**
* 使用 遍历 穷举 解决问题 万物算法皆是基于枚举
* @param height
* @return
*/
public int maxAreaTwo(int[] height) {
/*穷举是所有算法的核心*/
if (height == null || height.length ==0){
return 0;
}
int m = height.length;
int sum = 0 ;
for (int i = 0; i < m; i++) {
int num1 = height[i];
for (int j = 0; j < m; j++) {
int num2 = height[j];
int temp = Math.min(num1,num2)*(i-j);
sum = Math.max(sum,temp);
}
}
return sum;
}
}