brute force O(n2)
从头尾两头扫面 O(n)
1 public class Solution { 2 public int maxArea(int[] height) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int start = 0; 6 int end = height.length - 1; 7 int max = 0; 8 if(height == null) 9 return 0; 10 if(height.length < 2) 11 return 0; 12 while(start < end) 13 { 14 int tmp = (end - start) * Math.min(height[start], height[end]); 15 if(tmp > max) 16 max = tmp; 17 if(height[start] >= height[end]) 18 { 19 int oldend = end; 20 do{end--;} while(start < end&&height[end] < height[oldend]); 21 } 22 else 23 { 24 int oldstart = start; 25 do{start++;}while(start < end&&height[start] < height[oldstart]); 26 27 } 28 } 29 return max; 30 31 } 32 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步