https://leetcode.com/contest/6/problems/trapping-rain-water-ii/

看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想。

这题是google apactest 2017 round A 的第二题。https://code.google.com/codejam/contest/11274486/dashboard#s=p1

然后,简单一次调试,就ac了。不知道这算不算作弊,做完,我就看见什么instruction,说是可以看别人代码,然后举报作弊,有点怕!

分析:这题算是动态规划吧,读懂题意后,(其实这个题目描述的不清楚啊,图的示例倒是挺好),可以观察,可以从外圈往里圈缩,因为这个过程墙的高度是非递减的,然后,你应该想到用优先队列(priority_queue或者set,又是讨厌的set),先把外圈所有点加入,然后取出高度最小的点,更新四周的点,注意标记这个点是否访问过,这个过程中记录墙增加的高度就是最后的积水量。

哎!咸鱼也是有梦想的!

 int dx[] = {-, , , };
int dy[] = {, , , -};
class Solution {
public: int trapRainWater(vector<vector<int>>& h) {
int n = h.size();
if(n == ) return ;
int m = h[].size();
vector<vector<bool> > vis(n, vector<bool>(m, ));
priority_queue<pair<int, pair<int, int> > > q;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if(i == || j == || i == n - || j == m - ) {
vis[i][j] = ;
q.push({-h[i][j], {i, j}});
}
}
}
long long res = ;
while(!q.empty()) {
int u = -q.top().first;
int ux = q.top().second.first;
int uy = q.top().second.second;
q.pop();
//cout << ux << " " << uy << " " << u << endl;
for (int i = ; i < ; i++) {
int x = ux + dx[i];
int y = uy + dy[i];
if(x < || y < || x >= n || y >= m || vis[x][y])
continue;
if(h[x][y] < u) {
res += u - h[x][y];
h[x][y] = u;
}
vis[x][y] = ;
q.push({-h[x][y],{x, y} });
}
}
return res;
}
};
   int a[][];
bool v[][];
int dx[] = {, -, , };
int dy[] = {, , , -}; class Solution {
public:
bool in(int x, int y, int r, int c) {
return <= x && x < r && <= y && y < c;
}
int trapRainWater(vector<vector<int>>& h) {
priority_queue<pair<int, pair<int, int> > > q;
int m = h.size();
if(m == ) return ;
int n = h[].size(); memset(a, , sizeof a);
memset(v, , sizeof v);
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if(i == || j == || i == m - || j == n - ) {
q.push(make_pair(-h[i][j], make_pair(i, j)));
a[i][j] = h[i][j];
v[i][j] = ;
}
}
}
// cout << n << " " << m << endl;
while(q.size()) {
pair<int, pair<int, int> > u = q.top();
q.pop();
int x = u.second.first;
int y = u.second.second;
for (int k = ; k < ; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (in(nx, ny, m, n) && !v[nx][ny]) {
if (h[nx][ny] < a[x][y]) {
a[nx][ny] = a[x][y];
} else {
a[nx][ny] = h[nx][ny];
}
v[nx][ny] = ;
q.push(make_pair(-a[nx][ny], make_pair(nx, ny)));
}
}
}
int ans = ;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
ans += a[i][j] - h[i][j];
// printf("%d ", a[i][j]);
}
// printf("\n");
}
return ans;
}
};

[leetcode] 407. Trapping Rain Water II的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. 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 ...

  5. 407 Trapping Rain Water II 接雨水 II

    给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...

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

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

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

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

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

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

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

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

随机推荐

  1. Delphi- 操作EXCEL

    因工作需要,需要到操作EXCEL,先了解一下怎么读取EXCEL这个,做了一个DEMO,备注在这里 一.读取EXCEL unit Unit1; interface uses Windows, Messa ...

  2. VS项目重命名工具

    VS项目重命名工具 VS项目整体重命名工具 不再为项目重命名和修改命名空间而烦恼,简单几个字,但是开发加上测试大量项目,前前后后竟然跨越了1个月,汗...不过真正的开发时间可能2-3天的样子.  一. ...

  3. vs2005升级到vs2010相关问题

    1.项目编译失败,报 Resgen.exe 退出 ,错误代码2 处理方式: http://jingyan.baidu.com/article/90895e0fe80c6064ed6b0b6b.html ...

  4. cc2530 timer 3 PWM <可调占空比>

    前提: 开始用的是 cc2530 timer 1来做PWM的,已经可调占空比了,但是由于硬件的改动,需要用timer 3 和 timer 4 代替.由于调试过程中出了些小问题,于是自己把这个贴出来.关 ...

  5. mysql事务回滚

    首先条件是表要设置为 InnoDB  类型. 当在一个库连接中,通过调用另一个 库名称.表名称,可以回滚: 当用USE dbName后,在两个或多个库操作时,一次只能回滚一个库中的东西: 当在多个数据 ...

  6. Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    自从IOS7后UINavigationBar的一些属性的行为发生了变化.你可以在下图看到: 现在,如果你要修改它们的颜色,用下面的代码: self.navigationController.navig ...

  7. 对JavaScript对象数组按指定属性和排序方向进行排序

    引子 在以数据为中心的信息系统中,以表格形式展示数据是在常见不过的方式了.对数据进行排序是必不可少的功能.排序可以分为按单个字段排序和按多个字段不同排序方向排序.单字段排序局限性较大,不能满足用户对数 ...

  8. java1.5新特性

    一:增强for循环 增强for的格式:for(数据类型    变量名:集合或者 数组){              } 传统的for循环可以对数组进行遍历: for(int x=0;x { Syste ...

  9. javascript原型链简单的理解

    在JavaScript中,一共有两种类型的值,原始值和对象值.每个对象都有一个内部属性[prototype],我们通常称之为原型.原型的值可以是一个对象,也可以是null.当然也可能是一个值,如果它的 ...

  10. 关于Http协议的解析

    HTTP概述 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. HTTP就是一个通信规则,通信规则规定 ...