11 questions, most water filled containers

I, entitled 1

Here Insert Picture Description

Second, the idea

Traverse, the maximum output.

Individual optimization ideas: traversal time away from both ends toward the middle.

Third, the code

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. Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/container-with-most-water
    copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎

Published 48 original articles · won praise 1 · views 851

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/104160670