forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxArea.java
More file actions
26 lines (23 loc) · 692 Bytes
/
MaxArea.java
File metadata and controls
26 lines (23 loc) · 692 Bytes
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
package array;
import java.util.Arrays;
public class MaxArea {
public static void main(String[] args) {
int[] nums = new int[]{1,8,6,2,5,4,8,3,7};
int r = maxArea(nums);
System.out.println("最大面积为:"+r);
}
private static int maxArea(int[] nums) {
System.out.println("输入的数组为:"+ Arrays.toString(nums));
int l = 0,r=nums.length-1;
int ans = 0;
for (int i = 0; i < nums.length; i++) {
int area = Math.min(nums[l],nums[r])*(r-l);
ans = Math.max(ans,area);
if(nums[l] <= nums[r]){
++l;
}
--r;
}
return ans;
}
}