1. 程式人生 > >LeetCode Container With Most Water 查詢容水量最大的容器 動態規劃法思想分析

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 two endpoints of lineiis 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.

注意這裡的Note:和裝的東西是水,就是最短木板原理:一個桶能裝多少水是由最短的那塊木板決定的。

一開始沒搞清楚這一點,誤以為是比較梯形的面積,所以做錯了。

其實這個是動態規劃法做的,因為我沒看到有人分析其中的動態規劃法思想,所以在這裡分析一下。

下面分析是怎麼樣的動態規劃法思想。

1 分割槽,首先分最大區間,左邊高度為height[0],右邊高度為height[n-1], 分一個區間,那麼這個區間的解肯定是height[0]和height[n-1]圍成的區間

2 區間細分,分兩個區間height[0]和height[n-2],還有height[1]和height[n]也構成一個區間,這個區間的最大木桶容水量和前面比較,這個值也可以用表儲存下來,但是因為我們不需要中間結果,所以不需要儲存一個表,只需要儲存最大值就可以了。

3 繼續細分為3個區間,然後4個區間,……直到n-1個區間。

但是這道題就巧妙的利用了一個木桶和裝水的特徵,可以更加優化一點。不過其實也是動態規劃法原理。至於怎麼利用這個特徵,很多部落格都有分析了,我就不在這裡重複了。隨便百度就可以了。我只是沒看到有人分析這裡的動態規劃法的思想,所以分析一下。

看看LeetCode上有人貼出的程式:

int maxArea(vector<int> &height) {
		int i = 0;
		int j = height.size() - 1;
		int res = 0;
		while(i < j)
		{
			int temp = min(height[i], height[j]) * (j - i);
			if(temp > res)
				res = temp;
			if(height[i] <= height[j])
				i++;
			else
				j--;
		}
		return res;
	}

相關推薦

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 Java版(盛水容積)

11. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represen

[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 - Container With Most Water、Largest Rectangle in Histogram - 直方圖中面積問題

本文會總結以下兩道題目: 11 - Container With Most Water - 直方圖中盛水問題 (Medium) 84 - Largest Rectangle in Histogram - 直方圖中最大矩形面積問題 (Hard) 還有兩道題目也是直方圖中

每天一道程式設計題 (補) LeetCode-Container With Most Water

LeetCode上的題目太多了,每道都寫bolg實在是沒必要。接下來,小天會主要講解有趣的和重要的題目,這裡的重要是指題目有很大可能出現在面試中。 題目簡介: 這裡對題目作進一步形式化定義,給定一組非負整型數,題目希望找到兩個數字,使得目標函式最小。 舉例說明:輸入一組

leetCode-Container With Most Water-javascript

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

[LeetCode] 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: container-with-most-water

題目描述: 給定n個非負整數a1,a2,…,an,其中每個數字表示座標(i, ai)處的一個點。以(i,ai)和(i,0)(i=1

22.Container With Most Water(能裝多水的容器

圖片 int HERE ted 什麽 坐標 image pre ins Level: ??Medium 題目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point a

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

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】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 題目 思路和解答 思路

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

【 英文練習 | 中文練習 】 題目描述: 給定 n 個非負數 a1,a2,…,an,每個數代表座標中的一個點 (i,ai)。在座標內畫 n 條垂直線,垂直線 i 的兩個端點分別為 (i,ai) 和 (i,0)。找出其中的兩條線,使得它們與 x 軸共同構成的容器可以容納最多的水。

LeetCode】#11裝多水的容器(Container With Most Water)

【LeetCode】#11裝最多水的容器(Container With Most Water) 題目描述 給定 n 個非負整數 a1,a2,…,an,每個數代表座標中的一個點 (i, ai) 。在座標內畫 n 條垂直線,垂直線 i 的兩個端點分別為 (i, ai) 和 (i, 0)。

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

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