题11、盛最多水的容器

一、题目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