11件の質問、ほとんどの水を充填された容器

題しI、1

ここに画像を挿入説明

第二に、アイデア

トラバース、最大出力。

個々の最適化のアイデア:離れて中央に向かって両端からトラバーサル時間。

第三に、コード

public class T011 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] height = {1,8,6,2,5,4,8,3,7};
		System.out.println( maxArea(height) );		//49

	}

	public static int maxArea(int[] height) {

		int area = 0;

		for ( int i = 0; i < height.length; i++ ){

			for ( int j = i+1; j < height.length; j++ ){

				int tmp = (j-i)*Math.min( height[i], height[j] );

				if ( tmp > area )
					area = tmp;
			}
		}

		return area;
	}
}

  1. 出典:滞在ボタン(LeetCode)
    リンクします。https://leetcode-cn.com/problems/container-with-most-water
    すべてのネットワークからの控除が著作権を保有。商業転載は、ソースを明記してください許可公式、非商用の転載をご連絡ください。↩︎

公開された48元の記事 ウォンの賞賛1 ビュー851

おすすめ

転載: blog.csdn.net/weixin_45980031/article/details/104160670