11-container-with-most-water

题目描述:

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

代码解答:

package com.jack.algorithm;

/**
 * create by jack 2018/10/26
 *
 * @auther jack
 * @date: 2018/10/26 22:39
 * @Description: 水容器的最大值
 */
public class ContainerWithMostWater {


    /**
     * 题目描述:
     * https://leetcode.com/problems/container-with-most-water/
     *
     * @param height
     * @return
     */
    public static int maxArea(int[] height) {
        //最大值
        int max = 0;
        //数组长度
        int length = height.length;
        //计算容器最大值,依次比较
        for (int i = 0; i < length - 1; i++) {
            int h1 = height[i];
            for (int j = i + 1; j < length; j++) {
                int h2 = height[j];
                int w = j - i;
                if (h1 >= h2) {
                    if (max < h2 * w) {
                        max = h2 * w;
                    }
                } else {
                    if (max < h1 * w) {
                        max = h1 * w;
                    }
                }
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int[] a = new int[]{1,8,6,2,5,4,8,3,7};
        int c = maxArea(a);
        System.out.println("c = "+c);
    }
}

源码地址:

源码

猜你喜欢

转载自blog.csdn.net/wj903829182/article/details/83421865