[leetcode] 407. Trapping Rain Water II
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的更多相关文章
- [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] 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 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 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 ...
- 407 Trapping Rain Water II 接雨水 II
给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 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),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
随机推荐
- GridView控件显示图片
与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式 ...
- EventBus的使用,初学EventBus传值
一.概述 EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间 ...
- ASP.NET 学习的总结
应用程序域 使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做 ...
- CentOS 6.5 下载地址
CentOS 6.5 主要改动 Precision Time Protocol(精确时间协议)—— 原先是项技术预览 —— 现在已获全面支持.以下驱动程序支持网络时间戳印:bnx2x.tg3.e100 ...
- C++ 构造和析构
1.继承关系可认为,子类在父类的基础上进行.从这个角度讲,可把它认为穿衣脱衣的过程.穿衣是:先穿内衣,再穿外套.脱衣是:先脱外套,在脱内衣.构造是:先调用父类构造方法,再调用子类构造方法.析构是:先调 ...
- VK Cup 2015 - Finals, online mirror D. Restructuring Company 并查集
D. Restructuring Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Andorid4.x 流氓式屏蔽HOME键
转载请列明出处 http://blog.csdn.net/steelychen/article/details/37757341 应用项目须要要屏蔽HOME键. 项目本身的要求是让按下HOME键后程序 ...
- javax.naming.NameNotFoundException:Name[ XXX] is not bound in this context.
在用局部数据源去连数据库的时候,在本地的项目中,都是可以的,可是一部署到服务器上,就报错了. 报的错误是:javax.naming.NameNotFoundException:Name[ XXX] i ...
- 安卓服务(Service)的两种开启方式以及服务的生命周期
安卓中服务的开启方式 一:採用start的方式开启服务 调用函数:startService(Intent)->onCreate()->onStart()/onStartCommand()- ...
- iOS开发——总结篇&IOS开发基础知识
IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...