leetcode個人題解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { int max = 0; int l = 0; int r = height.size()-1; while(l < r) { int h = min(height[l], height[r]); int area = h * (r-l); if (area > max) max= area; while (height[l] <= h && l < r) l++; while (height[r] <= h && l < r) r--; } return max; } };
思路:先計算最寬的面積,當中間每一個容器高度小於當前兩端最低點時,肯定是不符合條件的,因為寬也減少了,略過即可。
leetcode個人題解——#11 Container with most water
相關推薦
leetcode個人題解——#11 Container with most water
lee most 中間 size etc 當前 ner 計算 area class Solution { public: int maxArea(vector<int>& height) { int max = 0;
《LeetBook》leetcode題解(11):Container With Most Water[M] ——用兩個指標在陣列內移動
我現在在做一個叫《leetbook》的免費開源書專案,力求提供最易懂的中文思路,目前把解題思路都同步更新到gitbook上了,需要的同學可以去看看 書的地址:https://hk029.gi
LeetCode 學習記錄 11. Container With Most Water
1 題目要求: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn s
leetcode解題之 11. Container With Most Water Java版(最大盛水容積)
11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represen
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 Notes_#11 Container with Most Water
LeetCode Notes_#11 Container with Most Water LeetCode Contents 題目 思路和解答 思路
【LeetCode】11. Container With Most Water - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given n non-negative integers a[1], a[2], …, a[n] , where each represent
【LeetCode】11. Container With Most Water(盛最多水的容器)-C++實現的三種方法
本題是Bloomberg的面試題。 問題描述: 一、第一種方法-暴力解法 當我們在面試時想不到解題的方法時,不妨使用暴力解法,雙重遍歷陣列。 當 i = 0 時,使用指標 j 遍歷陣列,找到第一輪的最大值 area: 當i = 2 ,使用指標 j 遍歷
【Leetcode】11. 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
【leetcode】11.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
Leetcode 11. Container With Most Water
如何盛最大的水? 陣列代表高度, 盛的水量V= min( height[left] 、 height[right] ) * 底部的長度= [right- left] 雙指標解決這個問題, 從左邊、右邊不
11 Container With Most Water的數學證明 | LeetCode
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盛最多水的容器
題目: 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(C++)
題目: Given n non-negative integers a1,a2,…,ana_1, a_2, \dots, a_na1,a2,…,an, where each represents a point at coordinate (i,ai)(
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