<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>算法</title>
</head>
<body><script>/** 1:盛水最多容器 **/const heightsArray = [4,8,1,2,3,9];// 时间复杂度 O(n^2) 空间复杂度: O(1)const getMaxWaterContainer1 = (heights) => {let maxArea = 0;for(let p1 = 0; p1 <heights.length; p1++) {for(let p2 = p1 + 1;p2<heights.length; p2++) {const height = Math.min(heights[p1], heights[p2]);const width= p2 - p1;const area = height * width;maxArea = Math.max(maxArea, area);}}return maxArea;};// 时间复杂度 O(n) 空间复杂度O(1)const getMaxWaterContainer2 = (heights) => {let p1 = 0,p2 = heights.length - 1,maxArea = 0;while (p1 < p2) {const height = Math.min(heights[p1], heights[p2]);const width = p2 - p1;const area = width * height;maxArea = Math.max(maxArea, area);if(heights[p1] <= heights[p2]){p1++;}else{p2--;}}return maxArea;};console.log(getMaxWaterContainer2(heightsArray));</script>
</body>
</html>