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),先把外圈所有点加入,然后取出高度最小的点,更新四周的点,注意标记这个点是否访问过,这个过程中记录墙增加的高度就是最后的积水量。

哎!咸鱼也是有梦想的!

  1. int dx[] = {-, , , };
  2. int dy[] = {, , , -};
  3. class Solution {
  4. public:
  5.  
  6. int trapRainWater(vector<vector<int>>& h) {
  7. int n = h.size();
  8. if(n == ) return ;
  9. int m = h[].size();
  10. vector<vector<bool> > vis(n, vector<bool>(m, ));
  11. priority_queue<pair<int, pair<int, int> > > q;
  12. for (int i = ; i < n; i++) {
  13. for (int j = ; j < m; j++) {
  14. if(i == || j == || i == n - || j == m - ) {
  15. vis[i][j] = ;
  16. q.push({-h[i][j], {i, j}});
  17. }
  18. }
  19. }
  20. long long res = ;
  21. while(!q.empty()) {
  22. int u = -q.top().first;
  23. int ux = q.top().second.first;
  24. int uy = q.top().second.second;
  25. q.pop();
  26. //cout << ux << " " << uy << " " << u << endl;
  27. for (int i = ; i < ; i++) {
  28. int x = ux + dx[i];
  29. int y = uy + dy[i];
  30. if(x < || y < || x >= n || y >= m || vis[x][y])
  31. continue;
  32. if(h[x][y] < u) {
  33. res += u - h[x][y];
  34. h[x][y] = u;
  35. }
  36. vis[x][y] = ;
  37. q.push({-h[x][y],{x, y} });
  38. }
  39. }
  40. return res;
  41. }
  42. };
  1. int a[][];
  2. bool v[][];
  3. int dx[] = {, -, , };
  4. int dy[] = {, , , -};
  5.  
  6. class Solution {
  7. public:
  8. bool in(int x, int y, int r, int c) {
  9. return <= x && x < r && <= y && y < c;
  10. }
  11. int trapRainWater(vector<vector<int>>& h) {
  12. priority_queue<pair<int, pair<int, int> > > q;
  13. int m = h.size();
  14. if(m == ) return ;
  15. int n = h[].size();
  16.  
  17. memset(a, , sizeof a);
  18. memset(v, , sizeof v);
  19. for (int i = ; i < m; i++) {
  20. for (int j = ; j < n; j++) {
  21. if(i == || j == || i == m - || j == n - ) {
  22. q.push(make_pair(-h[i][j], make_pair(i, j)));
  23. a[i][j] = h[i][j];
  24. v[i][j] = ;
  25. }
  26. }
  27. }
  28. // cout << n << " " << m << endl;
  29. while(q.size()) {
  30. pair<int, pair<int, int> > u = q.top();
  31. q.pop();
  32. int x = u.second.first;
  33. int y = u.second.second;
  34. for (int k = ; k < ; k++) {
  35. int nx = x + dx[k];
  36. int ny = y + dy[k];
  37. if (in(nx, ny, m, n) && !v[nx][ny]) {
  38. if (h[nx][ny] < a[x][y]) {
  39. a[nx][ny] = a[x][y];
  40. } else {
  41. a[nx][ny] = h[nx][ny];
  42. }
  43. v[nx][ny] = ;
  44. q.push(make_pair(-a[nx][ny], make_pair(nx, ny)));
  45. }
  46. }
  47. }
  48. int ans = ;
  49. for (int i = ; i < m; i++) {
  50. for (int j = ; j < n; j++) {
  51. ans += a[i][j] - h[i][j];
  52. // printf("%d ", a[i][j]);
  53. }
  54. // printf("\n");
  55. }
  56. return ans;
  57. }
  58. };

[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. cdll和windll的差别

    Python要想调用C语言写的动态连接库.不仅要兼容C接口的调用习惯,还须要兼容C语言的数据类型.幸运的是ctypes库已经做了这双方面的工作.以便调用动态连接库是很方便的.在Hello World的 ...

  2. Cocos2d-x 3.x 资料整理

     cocos2d-x-3.0rc0新project的分辨率设置和控制台输出信息 http://kome2000.blog.51cto.com/969562/1379704 Cocos2d-x 3. ...

  3. Array.prototype.slice.call(arguments) 类数组转成真正的数组

    Array.prototype.slice.call(arguments)   我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...

  4. Android 框架修炼-自己开发高效异步图片加载框架

    一.概述 目前为止,第三方的图片加载框架挺多的,比如UIL , Volley Imageloader等等.但是最好能知道实现原理,所以下面就来看看设计并开发一个加载网络.本地的图片框架. 总所周知,图 ...

  5. String 、InputStream、Reader 的转换

    1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes());orByteArrayInput ...

  6. C++的4种编程范型

    基于过程procedural-based 基于对象object-based 面向对象object-oriented 泛型技术generics

  7. 2.CentOS更换阿里源

    第一步:备份本地yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 第二步:下载阿里y ...

  8. java基础学习总结三(jdk7新特性、变量(局部变量和成员变量)、常量以及运算符)

    一:jdk7新特性 可以表示二进制数值,以0b开头,中间可以使用下划线_分隔符.如下: @Test /** * 测试jdk新特性 */ public void testJdk7(){ int a=0b ...

  9. Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)

    提示:python版本2.7,windows系统 Python提供的基本数据类型:空.布尔类型.整型.长整型.浮点型.字符串.列表.元组.字典.日期 1.空(None) None,是一个特殊的值,不能 ...

  10. FIFO分枝_限界算法

    问题: 检索4-皇后问题的状态空间树如下图的基本过程.(4-皇后问题解空间的树结构,结点按深度优先检索编号)  如果按序扩展这些结点,则下一个E-结点就是结点2.扩展结点2后生成结点3,8和13.利用 ...