leetcode解題之 11. Container With Most Water Java版(最大盛水容積)
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.
找兩條豎線然後這兩條線以及X軸構成的容器能容納最多的水。
假設先選取的是兩端之間的兩條線段,這樣這兩條線段之間的距離是最大的,長度是給定陣列的長度減1。那麼在這種情況下要容納更多的水,由於寬度已經是最大的了,只能想法提高線段的高度,這種情況下如果兩端是左邊比右邊高,那麼只有可能是將左邊的指標右移,否則將右邊的指標左移,然後這右回到了初始的問題,這樣不斷移動下去到左右指標相等為止
// leetcode此種方法有時TLE,有時AC public int maxArea1(int[] height) { if (height == null || height.length == 0) return 0; int left = 0; int right = height.length - 1; int maxArea = 0; while (left < right) { maxArea = Math.max(Math.min(height[left], height[right]) * (right - left), maxArea); if (height[left] <= height[right]) left++; else right--; } return maxArea; } // 優化版 public int maxArea(int[] height) { if (height == null || height.length == 0) return 0; int left = 0; int right = height.length - 1; int maxArea = 0; while (left < right) { // 設算當前的最大值 maxArea = Math.max(Math.min(height[left], height[right]) * (right - left), maxArea); if (height[left] <= height[right]) { int k = left; // 如果letf右邊的高度比left的高度小,面積不可能比之前大,所以 // 從[left, right - 1]中,從左向右找, //找第一個高度比height[left]高的位置 while (k < right && height[k] <= height[left]) k++; left = k; } else { int k = right; // 如果right左邊的高度比right的高度小,面積不可能比之前大,所以 // 從[left + 1, right]中,從右向左找, //找第一個高度比height[right]高的位置 while (k > left && height[k] <= height[right]) k--; // 從[left, right - 1]中,記錄第一個比原來height[right]高的位置 right = k; } } return maxArea; }
相關推薦
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 - 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
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)
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
lee most 中間 size etc 當前 ner 計算 area class Solution { public: int maxArea(vector<int>& height) { int max = 0;
LeetCode Container With Most Water 查詢容水量最大的容器 動態規劃法思想分析
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the t
【LeetCode】11. Container With Most Water 解題報告
Subject Given n non-negative integers a1, a2, …, an, where each represents a point at c
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(盛最多水的容器)-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
leet code 11 Container With Most Water golang解題
理解題意:一個數組i,a[i]形成一個高度,找到兩個高度之間能裝最多水的面積。 思路: 從兩邊向中間找(不考慮高度的情況下,長度最大) 從比較小的開始向中間找比他大的(比他小的容積不可能更大) f
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