1. 程式人生 > >LeetCode --- 11. Container With Most Water

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 such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

這道題的要求是給定整形陣列a,(i, ai)表示座標點,這些座標點向x軸作垂直的線。找到兩條線,使其和x軸共同構成的容器,可以裝下最多的水。(注意,不允許傾斜容器)

簡單直接的思路是兩層迴圈遍歷每組可能的情況,時間複雜度O(n2)。可以進一步優化,採用兩個指標l和r,初始化分別指向陣列的兩端,然後在向中間移動找到最大容量。如果l指向的數字小,則l需要右移才有可能獲得更大容量,因為此時如果左移r,得到的容量肯定比左移r之前的容量小(高度已經被較小的l限制住了)。如果r指向的數字小,則需要左移r。這樣,當l和r相遇的時候,最大的容量就是我們需要的。

時間複雜度:O(n)

空間複雜度:O(1)

 1 class Solution
 2 {
 3 public:
 4     int maxArea(vector<int> &height)
 5     {
 6         int l = 0, r = height.size() - 1, res = 0;
 7         while(l < r)
 8         {
 9             res = max(res, (r - l) * min(height[l], height[r]));
10             if(height
[l] < height[r]) 11 ++ l; 12 else 13 -- r; 14 } 15 return res; 16 } 17 };

相關推薦

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

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] 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

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

Leetcode 11. Container With Most Water

如何盛最大的水? 陣列代表高度, 盛的水量V= min( height[left] 、 height[right] ) * 底部的長度= [right- left] 雙指標解決這個問題, 從左邊、右邊不

leetcode-11:Container With Most Water盛最多水的容器

題目: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, 

leetCode 11 Container With Most Water

題目描述(中等難度) 每個陣列代表一個高度,選兩個任意的柱子往裡邊倒水,能最多倒多少水。 解法一 暴力解法 直接遍歷任意兩根柱子,求出能存水的大小,用一個變數儲存最大的。 public int maxArea(int[] height) { int

LeetCode 11. Container With Most Water(java)

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two end

[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 such that the two endp

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

轉向c++了,底乘高,低最大,面積再想大一點就得 class Solution { public: int maxArea(vector<int>& height) { int maxcon=0; int i=0; i

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 such that the two endpoint

LeetCode-11-Container With Most Water

sla eas max nta height ive most raw coo 算法描述: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate

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

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

[C++]LeetCode 11: Container With Most Water(最大容積/最大矩形面積)

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

LeetCode Notes_#11 Container with Most Water

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

LeetCode11. Container With Most Water - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given n non-negative integers a[1], a[2], …, a[n] , where each represent

LeetCode11. Container With Most Water(盛最多水的容器)-C++實現的三種方法

本題是Bloomberg的面試題。 問題描述:  一、第一種方法-暴力解法   當我們在面試時想不到解題的方法時,不妨使用暴力解法,雙重遍歷陣列。 當 i = 0 時,使用指標 j 遍歷陣列,找到第一輪的最大值 area: 當i = 2 ,使用指標 j 遍歷

Leetcode11. Container With Most Water (medium)

11. Container With Most Water (medium) 描述 Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical l

leetcode11.Container With Most Water(c語言)

Description: Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines a