凹槽最大水容量

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

思路:

对某个值A[i]来说,能trapped的最多的water取决于在i之前最高的值leftMostHeight[i]和在i右边的最高的值rightMostHeight[i](均不包含自身)。

如果min(left,right) > A[i],那么在i这个位置上能trapped的water就是min(left,right) – A[i]。

有了这个想法就好办了,第一遍从左到右计算数组leftMostHeight,第二遍从右到左计算rightMostHeight。
时间复杂度是O(n)。

public class Solution {
     public int trap(int[] A) {
if(A == null || A.length < 1)
            return 0;  
          
        int maxheight = 0; 
        int[] leftMostHeight = new int[A.length];  
        for(int i =0; i < A.length; i++)  {  
            leftMostHeight[i] = maxheight;  
            maxheight = maxheight > A[i] ? maxheight : A[i];  
        }  
  
        maxheight = 0;  
       int[] rightMostHeight = new int[A.length];  
        for(int i =A.length - 1; i>=0; i--) {  
            rightMostHeight[i] = maxheight;  
            maxheight = maxheight > A[i] ? maxheight : A[i];  
        }  
  
        int water = 0;  
        for(int i =0; i < A.length; i++)  
        {  
            int high = Math.min(leftMostHeight[i], rightMostHeight[i]) - A[i];  
            if(high>0)  
                water += high;  
        }  
        return water;

}

}


解法二:

是两边往中间遍历,
记录当前第二高点secHight,
然后利用这个第二高点减去当前历经的柱子,
剩下就装水容量了;
两边比较时,最高的点不用动,只移动第二高点  
    public class Solution {
     public int trap(int[] A) {    

  int secHight = 0;
int left = 0;
int right =A.length;
int area = 0;
while (left < right){
if (A[left] < A[right]){
secHight = Math.max(A[left], secHight);
area += secHight-A[left];//计算当前格的能装雨水的容量
left++;
} else {
secHight = Math.max(A[right], secHight);
area += secHight-A[right];
right--;
}
}
return area;

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是微信小程序底部导航中间凹槽的实现方法: 1. 在app.json文件中设置tabBar的样式,将中间按钮的位置留出来,例如: ```json "tabBar": { "color": "#999999", "selectedColor": "#1296db", "backgroundColor": "#ffffff", "borderStyle": "black", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/tabbar/home.png", "selectedIconPath": "images/tabbar/home_active.png" }, { "pagePath": "pages/mine/mine", "text": "我的", "iconPath": "images/tabbar/mine.png", "selectedIconPath": "images/tabbar/mine_active.png" }, { "pagePath": "pages/center/center", "text": "", "iconPath": "images/tabbar/center.png", "selectedIconPath": "images/tabbar/center_active.png" }, { "pagePath": "pages/cart/cart", "text": "购物车", "iconPath": "images/tabbar/cart.png", "selectedIconPath": "images/tabbar/cart_active.png" }, { "pagePath": "pages/category/category", "text": "分类", "iconPath": "images/tabbar/category.png", "selectedIconPath": "images/tabbar/category_active.png" } ] } ``` 2. 在TabBar组件中添加中间按钮的样式,例如: ```css .tabbar-center { position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); z-index: 999; width: 60rpx; height: 60rpx; border-radius: 50%; background-color: #1296db; box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.15); } ``` 3. 在TabBar组件中添加中间按钮的点击事件,例如: ```javascript methods: { onCenterClick() { wx.navigateTo({ url: '/pages/center/center' }) } } ``` 4. 在TabBar组件中添加中间按钮的图片和文字,例如: ```html <view class="tabbar-center" @tap="onCenterClick"> <image src="/static/images/tabbar/center.png" class="tabbar-center-icon"></image> <text class="tabbar-center-text">中心</text> </view> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值