Posted on    Last Updated On 2022-09-15 by zyrastory
內容目錄
LeetCode題目翻譯
英文原文如下
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
public class Solution {
public int MaxArea(int[] height) {
int l = height.Length;
int max = 0; //目前最大面積
int maxH = -1; //i 的最高高度
for(int i = 0; i<l-1;i++)
{
if(height[i] < maxH)
{
continue;
}
for(int j =i+1; j<l;j++)
{
int cal = Math.Min(height[i],height[j]) * (j-i); //兩條線高度取其低 * index差
if(cal>max)
{
maxH = height[i];
max = cal;
}
}
}
return max;
}
}
時間複雜度 : O(n2)
很基礎的巢狀迴圈 ,看到 i 跟 j 的時候應該就猜到了吧哈哈
這裡可以注意到多用了一個 maxH 來儲存目前最大容積的 i 高度(第一條的高度)
maxH 為目前最大值的i 高度,要是判斷下次迴圈時, i高度沒有高於maxH,就直接跳過
提交LeetCode執行速度約為 500~700ms左右,算是挺糟糕的
⭐方案2 – 推薦寫法,two pointer 前後收斂
過了一陣子之後,在回顧寫過的題目時,實在是嚥不下那口氣
苦思冥想過後,又更新了寫法,依然用兩個參數 i跟j,但不做兩次遞迴,改用前後收斂的方式
public class Solution {
public int MaxArea(int[] height) {
int i = 0; //index的前收斂值
int j = height.Length-1; //index的後收斂值
int maxAmt = 0;
int iH;
int jH;
int cal;
while(j>i)
{
iH = height[i];
jH = height[j];
//從前後開始收斂
//哪一邊比較高,下次就收斂另一邊,一樣的話我們就委屈 i 一點吧
if(iH>jH)
{
cal = jH*(j-i);
j--;
}
else
{
cal = iH*(j-i);
i++;
}
if(cal>maxAmt)
maxAmt = cal;
}
return maxAmt;
}
}
class Solution {
public int maxArea(int[] height) {
int i = 0;
int j = height.length-1;
int maxAmt = 0;
int iH;
int jH;
int cal;
while(j>i)
{
iH = height[i];
jH = height[j];
//Convergence back and forth
//which is higher,than will convergence from the other side
if(iH>jH)
{
cal = jH*(j-i);
j--;
}
else
{
cal = iH*(j-i);
i++;
}
if(cal>maxAmt)
maxAmt = cal;
}
return maxAmt;
}
}
Python3 解決方案
方案1
class Solution:
def maxArea(self, height: List[int]) -> int:
i = 0;
j = len(height)-1;
maxAmt = 0;
while(j>i):
iH = height[i];
jH = height[j];
if(iH>jH):
cal = jH*(j-i);
j-=1;
else:
cal = iH*(j-i);
i+=1;
if(cal>maxAmt):
maxAmt = cal;
return maxAmt;
JavaScript 解決方案
方案1
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
var i = 0;
var j = height.length-1;
var maxAmt = 0;
var iH;
var jH;
var cal;
while(j>i)
{
iH = height[i];
jH = height[j];
if(iH>jH)
{
cal = jH*(j-i);
j--;
}
else
{
cal = iH*(j-i);
i++;
}
if(cal>maxAmt)
{
maxAmt = cal;
}
}
return maxAmt;
};
結論
這題被歸類在LeetCode中階的題目中,但其實解法也不算複雜(就算暴力計算也能得到結果)
但 two pointer 這個寫法可以大大減少時間的消耗,之後在Array類的題目都會蠻常有他出場的機會
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};