博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
11. Container With Most Water && 42. Trapping Rain Water
阅读量:5370 次
发布时间:2019-06-15

本文共 3255 字,大约阅读时间需要 10 分钟。

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.

Hide Tags
 
 
Hide Similar Problems
 
public class Solution {    public int maxArea(int[] height) {        int leftInd = 0;        int rightInd = height.length-1;                int leftMax = 0;        int rightMax = 0;        int areaAax = 0;        while(leftInd

 

 
 
 
 

42. Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example, 

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Thanks Marcos for contributing this image!

 
Hide Tags
 
  
Hide Similar Problems
 
 
 
 
public class Solution {    public int trap(int[] height) {        if(height.length == 0)            return 0;        //Create a "skyline" such that        //1.the left part of this skyline is based on the max value ever seen to the left        //2.the right part of this skyline is based on the max value ever seen to the right        //3. two parts merge at the maximum point.                int[] leftBasedMax = new int[height.length];        leftBasedMax[0] = height[0];        int maxIndex = 0;        for(int i = 1; i
height[maxIndex]) { leftBasedMax[i] = height[i]; maxIndex = i; } else leftBasedMax[i] = leftBasedMax[i-1]; } //Improve: you can save an array space, if you do an extra iteration first to find the max value index. int[] rightBasedMax = new int[height.length]; rightBasedMax[height.length-1] = height[height.length-1]; for(int i = height.length-2; i>maxIndex; --i) { rightBasedMax[i]=Math.max(rightBasedMax[i+1],height[i]); } int sum = 0; for(int i = 0; i<=maxIndex; ++i) sum+=leftBasedMax[i]-height[i]; for(int i = maxIndex+1; i

 

More improvements: Use two pointers, the left pointer go right-ward, and the right pointer go left-ward.

public class Solution {    public int trap(int[] height) {        if(height.length == 0)            return 0;                    int left = 0;        int right = height.length - 1;        int sum = 0;        int leftMax = height[left];        int rightMax = height[right];        while (left < right) {          //The water can be guaranteed to hold at least by two ends          //Just move the shorter end index inward.          if (leftMax < rightMax) {            sum += leftMax - height[left];            ++left;            leftMax = Math.max(leftMax, height[left]);          } else {            sum += rightMax - height[right];            --right;            rightMax = Math.max(rightMax, height[right]);          }        }        return sum;    }}

 

 

转载于:https://www.cnblogs.com/neweracoding/p/5649764.html

你可能感兴趣的文章
sim usim Uim 区别
查看>>
网页中插入透明Flash的方法和技巧
查看>>
动态内存申请函数选择(realloc、malloc 、alloca、 calloc)
查看>>
获取元素属性get_attribute
查看>>
Python/jquery
查看>>
【BZOJ】【2132】圈地计划
查看>>
Java有没有goto?
查看>>
求不相邻金币相加和的最大值--动态规划1
查看>>
[转][osg]探索未知种族之osg类生物【目录】
查看>>
四十九. Zabbix报警机制 、 Zabbix进阶操作 、 监控案例
查看>>
元类中__new__ 与 __init__的区别--day27
查看>>
占小狼的简书博客
查看>>
struts2__action执行顺序
查看>>
php异常处理
查看>>
[xampp] /usr/bin/env: php: No such file or directory
查看>>
细学PHP 10 贴吧-2
查看>>
黑客攻防入门秘籍
查看>>
Swift迎来了1.0 GM 版(2014.09.09)
查看>>
【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查...
查看>>
《Genesis-3D开源游戏引擎-FQA常见问题解答》2014年01月10号版本
查看>>