https://oj.leetcode.com/problems/trapping-rain-water/

模拟题,计算出在凹凸处存水量。

对于一个位置 i ,分别计算出它左边的最大值 left (从左扫描一遍), 右边的最大值 right(从右扫描一遍) 。找left right中的最小值,如果大于 A[i],则做 min - A[i] 的累加。

class Solution {
public:
int trap(int A[], int n) {
if(n<=)
return ; vector<int> leftHigher;
vector<int> rightHigher;
int leftLarge = , rightLarge = ;
for(int i = ;i<n;i++)
{
if(A[i]>leftLarge)
leftLarge = A[i];
leftHigher.push_back(leftLarge);
}
rightHigher.resize(n);
for(int j = n-;j>=;j--)
{
if(A[j]>rightLarge)
rightLarge = A[j];
rightHigher[j] = rightLarge;
} int sum = ;
for(int i = ;i<n;i++)
{
int min = leftHigher[i]<rightHigher[i]?leftHigher[i]:rightHigher[i];
if(min>A[i])
sum += min - A[i];
} return sum;
}
};

LeetCode OJ-- Trapping Rain Water*的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. [LeetCode] 407. Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  7. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  8. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  9. LeetCode 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

  10. leetcode 【 Trapping Rain Water 】python 实现

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

随机推荐

  1. shell 练习 - 第七周

    1. 用shell实现传入进程pid, 查看对应进程/proc下CPU.内存指标 #!/bin/bash read -p "Input PID Value: " pid pid_e ...

  2. JS正则表达式学习总结

    JS正则:java RegExp对象,它是对字符串执行模式匹配的强大工具.运用最多的就是在输入处验证输入的字符串是否合法,指定用户输入字符串的格式. 定义方法: 1:直接量语法:var re=/pat ...

  3. Ubuntu 18.04 下用命令行安装Sublime

    介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...

  4. 02等待单个线程返回WaitForSingleObject

    windows 多线程之等待线程返回 ​ 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...

  5. Survey lists 10 most innovative cities

    From China Daily Beijing and Shanghai are among the 10 most innovative cities in the world, based on ...

  6. 通过session模拟登陆

    import requests # 这个练习对比的是上一个登陆练习,这个是不用自己传入cookie参数,而是利用session方法登陆 # 实例化一个session session = request ...

  7. R-barplot()

    barplot这个函数啊...坑...度娘的很多解决方案都不对,只好重新看回manual再做测试== 本文参考的是: https://stat.ethz.ch/R-manual/R-devel/lib ...

  8. SQL防止重复提交和Filter

    /class User package com.neuedu.bean; import java.io.Serializable; public class User implements Seria ...

  9. How to setup multimedia on CentOS 7

    You will need to also install the EPEL repository as nux-dextop depends on this for some of its pack ...

  10. Ubuntu 档案权限

    Linux文件属性:查看指令是:ls -al ls是list的意思,重点在显示档案的文件名与相关属性.而选项-al则表示列出所有的档案详细的权限与属性.