zoukankan      html  css  js  c++  java
  • leetcode container-with-most-water(medium) /java

    题目描述:

    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.

    import java.io.*;
    import java.util.*;
    
    public class Solution
    {
        public static  int maxArea(int[] height)
        {
            int len=height.length;
            int i=0,j=0,r=0;
            int len1=len-1;
            int[] c=new int[len1];
            for(i=0;i<len1;i++)
                c[i]=0;
            for(i=0;i<len1;i++)
            {
                for(j=i+1;j<len;j++)
                {
                    c[i]=Math.min(height[i],height[j])*(j-i);
                    if(c[i]>r)
                        r=c[i];
                }
    
            }
            return r;
        }
        public static void main(String[] args)
        {
            Scanner input=new Scanner(System.in);
            int[] h={1,2,1};
            int r=maxArea(h);
            System.out.println(r);
        }
    }

    暴力遍历。

    超时。

    应该只能有一层循环。

    【待更新】

     

     来自大神的代码。

  • 相关阅读:
    java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport
    project configuration is not up-to-date with pom.xml
    消息列队5
    消息列队4
    消息列队3
    聊聊常见的数据库架构设计方案?
    消息队列2
    消息队列1
    搜索引擎5
    搜索引擎4
  • 原文地址:https://www.cnblogs.com/zhenzhenhuang/p/6841192.html
Copyright © 2011-2022 走看看