LeetCode OJ-- Trapping Rain Water*
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*的更多相关文章
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [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 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
- leetcode 【 Trapping Rain Water 】python 实现
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
随机推荐
- shell 练习 - 第七周
1. 用shell实现传入进程pid, 查看对应进程/proc下CPU.内存指标 #!/bin/bash read -p "Input PID Value: " pid pid_e ...
- JS正则表达式学习总结
JS正则:java RegExp对象,它是对字符串执行模式匹配的强大工具.运用最多的就是在输入处验证输入的字符串是否合法,指定用户输入字符串的格式. 定义方法: 1:直接量语法:var re=/pat ...
- Ubuntu 18.04 下用命令行安装Sublime
介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...
- 02等待单个线程返回WaitForSingleObject
windows 多线程之等待线程返回 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...
- Survey lists 10 most innovative cities
From China Daily Beijing and Shanghai are among the 10 most innovative cities in the world, based on ...
- 通过session模拟登陆
import requests # 这个练习对比的是上一个登陆练习,这个是不用自己传入cookie参数,而是利用session方法登陆 # 实例化一个session session = request ...
- R-barplot()
barplot这个函数啊...坑...度娘的很多解决方案都不对,只好重新看回manual再做测试== 本文参考的是: https://stat.ethz.ch/R-manual/R-devel/lib ...
- SQL防止重复提交和Filter
/class User package com.neuedu.bean; import java.io.Serializable; public class User implements Seria ...
- 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 ...
- Ubuntu 档案权限
Linux文件属性:查看指令是:ls -al ls是list的意思,重点在显示档案的文件名与相关属性.而选项-al则表示列出所有的档案详细的权限与属性.