1. 程式人生 > >Container With Most Water--javascript解法

Container With Most Water--javascript解法

var maxArea = function(height) {
    let res = l = 0, r = height.length - 1, cur;
    while (l < r){
        cur = Math.min(height[l], height[r]) * (r - l);
        if (cur > res){
            res = cur;
        } 
        height[l] <= height[r] ? l += 1: r -= 1;
    }
    return res;
};

這是一道蠻有意思的題目,計算裝水的最大值,也是要遍歷,該題普通演算法遍歷所有情況需要O(n^2)時間複雜度,該解法時間複雜度O(n),也就是考慮了水桶兩邊的擋板,哪一個短,將該邊的位置往中移動,直到左右兩邊的擋板重合。實現遍歷,取出遍歷時候的最大值。

相關推薦

Container With Most Water--javascript解法

var maxArea = function(height) { let res = l = 0, r = height.length - 1, cur; while (l < r){ cur = Math.min(height[l],

leetCode-Container With Most Water-javascript

Container With Most Water 題目連結 題目描述: 給定n個非負整數 a1, a2, …, an , 每個數都代表座標軸上一個點 (i, ai).n條垂直線在 (i, ai) 和(i, 0)之間. 找出兩條線, 與x軸形成一個容器, 使得這個容器能裝下最

Leetcode 11. Container With Most Water 盛最多水的容器 解法

題目如下: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn suc

[LeetCode]11. Container With Most Water解法及python

原題連結:https://leetcode.com/problems/container-with-most-water 最開始的想法是O(n2n2)的,即暴力的雙層迴圈,比較哪兩條線圍成的梯形或矩形

leetcode題解||Container With Most Water問題

ble ret lee cpp 暴力 point amp form leetcode problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coo

LeetCode 11. Container With Most Water (裝最多水的容器)

cheng 個數 參考 找到 資料 算法題目 pointer etc html Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a

11. Container With Most Water

sla 其中 forms n-n 垂直 form amp pan else Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).

11 Container With Most Water (medium)

eth with ont 繪制 repr end lin con alt Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).

Container With Most Water

lis ati 復雜度 poi ner 遍歷 嵌套循環 超時 16px Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n

Leetcode:11- Container With Most Water

rms mos 谷歌 nat etc 翻譯 至少 leet each Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n

[Leetcode] Container With Most Water

當前 represent contains ble max 中間 sent points con Container With Most Water 題解 題目來源:https://leetcode.com/problems/container-with-most-wate

[LeetCode] 11. Container With Most Water 裝最多水的容器

OS python oge containe 較高的 contains tco water IT Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina

11. Container With Most Water 裝水最多的容器

異常 style http 指針 param post aps wid lose [抄題]: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate

Leetcode 11. Container With Most Water (two pointers)

oss else lee all pointer ide mov AR chan Leetcode: 11 there are two ways to deal with two pointers one is O(n), two pointers moves from b

11. Container With Most Water (Array)

current return 計算 int 去掉 ++ with pan solution O(n)的方法,兩邊開始計算最wide的container, 因為往中間走的話,要想遇到更max的,需要更高的smaller height,因此可以去掉已經計算過的smaller的h

最大容積 Container With Most Water

pre 技術 math 枚舉 算法 面積 時間 圖片 設置 2018-07-31 17:28:42 問題描述: 問題求解: 很容易想到的是Brute Force,也就是枚舉所有可能的pairs,這種解法的時間復雜度為O(n ^ 2),由於本題的數據規模較大,會TLE。那

[Swift]LeetCode11. 盛最多水的容器 | Container With Most Water

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertica

【LeetCode】118.Container With Most Water

題目描述(Medium) Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai).

LeetCode Notes_#11 Container with Most Water

LeetCode Notes_#11 Container with Most Water LeetCode  Contents 題目 思路和解答 思路

11. Container With Most Water C++

info col pub 技術 bubuko size area bsp alt 方法1:Brute Force(執行時間慘不忍睹,共進行) class Solution { public: int maxArea(vector<int>& h