LeedCode_はほとんど水コンテナを満たし

トピックの説明

所与のn非負整数A1、A2、...、ポイント(iは、AI)各座標の数を表します。垂直座標内の動画n行、私は2つのエンドポイントは、(i、AI)及び(I、0)は垂直線です。x軸の構成は、水を収容できるように一緒に容器を有する2本のラインを見つけます。
注:あなたは、コンテナを傾けることができない、とnの値が2以上です。
ここに画像を挿入説明

入力:[1,8,6,2,5,4,8,3,7]
出力:49

リンク:https://leetcode-cn.com/problems/container-with-most-water
分析:ダブルポインタ方式の
参考記事:https://leetcode-cn.com/problems/container-with-most-water /溶液/盛-ZUI-デュオ -shuiド栄-気バイleetcode /

int maxArea(vector<int>& height)
{
	int i = 0, j = height.size() - 1;
	if (j == -1)
		return 0;
	int res = 0;
	while (i < j)
	{
		if (height[i] < height[j])
		{
			res = max(res, (j - i) * height[i]);
			i++;
		}
		else
		{
			res = max(res, (j - i) * height[j]);
			--j;
		}
	}
	return res;
}
公開された63元の記事 ウォン称賛14 ビュー10000 +

おすすめ

転載: blog.csdn.net/luncy_yuan/article/details/104158815