zoukankan      html  css  js  c++  java
  • leetcode42 接雨水

    简介

    接雨水.

    简单思路

    排序, 依次选择最高的柱子,所围城的池塘高度

    code

    class Solution {
    public:
    
        struct zhuzi{
            int height;
            int index;
        };
        bool static cmp(const struct zhuzi &a, const struct zhuzi &b) {
            return a.height > b.height;
        }
        int trap(vector<int>& height) {
            if(height.size() < 2) {
                return 0;
            }
            int sum = 0;
            vector<struct zhuzi> zh;
            int index = 0;
            for(auto it : height) {
                zh.push_back({it, index});
                index++;
            }
            sort(zh.begin(), zh.end(), cmp);
            vector<bool> check(zh.size(), false);
            int minIndex = 0;
            int maxIndex = 0;
            for(int i =0; i < zh.size(); i++) {
                if(i == 0) {
                    int a = zh[i].height;
                    int b = zh[i+1].height;
                    int abminH = min(a, b);
                    int aI = zh[i].index;
                    int bI = zh[i+1].index;
                    int minab = min(aI, bI);
                    minIndex = minab;
                    int maxab = max(aI, bI);
                    maxIndex = maxab;
                    for(int j = minab + 1; j < maxab; j++){
                        sum += abminH - height[j];
                        check[j] = true;
                    }
                    i++;
                }else{
                    
                    int c = zh[i].height;
                    int ci = zh[i].index;
                    if(ci >= minIndex && ci <= maxIndex ) {
                        continue;
                    }
                    if(ci < minIndex) {
                        for(int j = ci + 1; j < minIndex; j++) {
                            sum += c - height[j];
                            check[j] = true;
                        }
                        minIndex = ci;
                    }else if(ci > maxIndex){
                        for(int j = maxIndex + 1; j < ci; j++) {
                            sum += c - height[j];
                            check[j] = true;
                        }
                        maxIndex = ci;
                    }
                }
            }
            return sum;
        }
    };
    
      class Solution {
        public int trap(int[] height) {
            if(height == null || height.length == 0) {
                return 0;
            }
            int ans = 0;
            int size = height.length;
            int [] left_max = new int[size];
            int [] right_max = new int[size];
            left_max[0] = height[0];
            for(int i=1; i<size; i++){
                left_max[i] = Math.max(height[i], left_max[i-1]);
            }
            right_max[size - 1] = height[size - 1];
            for(int i = size - 2; i>=0;  i--) {
                right_max[i] = Math.max(height[i], right_max[i + 1]);
            }
            for(int i=1; i<size - 1; i++) {
                ans += Math.min(left_max[i], right_max[i]) - height[i];
            }
            return ans;
        }
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Oracle_高级功能(9) 性能优化
    Oracle_高级功能(8) 事务和锁
    Oracle_高级功能(7) 数据字典视图和动态性能视图
    Oracle_高级功能(6) 分区
    Oracle_高级功能(5) 用户、角色、权限
    Oracle_高级功能(4) 数据库存储结构
    Oracle_高级功能(3) synonym和database link
    Oracle_高级功能(2) 索引
    hdu 1561 The more, The Better 树形dp
    C. Glass Carving 正着做或者倒着做都可以
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14772682.html
Copyright © 2011-2022 走看看