[抄题]:

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 灌水的多少基本上由左右边界中较矮的一块木板开始,由相邻是否有较高的木板决定。第一次学灌水,算了

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

灌水的多少基本上由左右边界的木板决定。

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

while (left < right) {
if (heights[left] < heights[right]) {
//start from left
left++;
if (left_max > heights[left]) {
result += (left_max - heights[left]);
}else {
left_max = heights[left];
}
}else {
//start from right
right--;
if (right_max > heights[right]) {
result += (right_max - heights[right]);
}else {
right_max = heights[right];
}
}
}

while (left < right)的基础上才能循环

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

装最多水的容器 · Container With Most Water 2指针

[代码风格] :

public class Solution {
/**
* @param heights: a list of integers
* @return: a integer
*/
public int trapRainWater(int[] heights) {
int result = 0;
//corner case
if (heights == null || heights.length == 0) {
return 0;
} int left = 0;
int right = heights.length - 1;
int left_max = heights[left];
int right_max = heights[right]; while (left < right) {
if (heights[left] < heights[right]) {
//start from left
left++;
if (left_max > heights[left]) {
result += (left_max - heights[left]);
}else {
left_max = heights[left];
}
}else {
//start from right
right--;
if (right_max > heights[right]) {
result += (right_max - heights[right]);
}else {
right_max = heights[right];
}
}
}
return result;
}
}

[抄题]:

Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining.

Given 5*4 matrix

[12,13,0,12]
[13,4,13,12]
[13,8,10,12]
[12,13,12,12]
[13,13,13,13]

return 14.

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 以为从四个角当作外围,朝中间过渡。应该把四堵墙当作外围,要能把中间都包起来才能做外围。
  2. 怎么想不到利用堆,要分析数据特点再选数据结构 而不是一个个套用数据结构:每次都是从q的顶点(最矮的点)从低往高注水,故用heap.
    1.   q中放的是能向四周扩展的cell, 但是每次都是从q的顶点(最矮的点)从低往高注水。不是从高往低duang地一下往下倒,没想象出来。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. PQ的括号里要传新建的引用new CellComparator(),体现参数的多态
  2. q中没有cell并往里头放时,新建即可
  3. 队列已经创建出对象,则不用null。再判断对象是否为零,因此用isempty()
  4. 从0 开始,取m个数,最后一个应该是m - 1,老是稍微理解了然后又忘了
  5. java int[] 未初始化时默认值是,integer[]未初始化时默认值是null
  6. int[][] visit = new int[n][m]; 已经忘写好几次了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

灌水(扩展类)本质还是BFS,区区几个算法中的一种。要分析数据特点再选数据结构 而不是一个个套用数据结构

[复杂度]:Time complexity: O(m*n个元素*lg(m+n)每个元素在heap中取最小值) Space complexity: O(m*n个点保存在去重矩阵中)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. BFS,“扩展类”问题就应该想到,印象还不够深
  2. dp好用,可不要贪杯哦
  3. 重写@override是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。重载(overloading) 是在一个类里面,方法名字相同,而参数不同。

[关键模板化代码]:

class CellComparator implements Comparator<Cell> {
public int compare(Cell a, Cell b) {
if (a.h > b.h) {
return 1;
}else if (a.h == b.h) {
return 0;
}else {
return -1;
}
}
}

自制CellComparator

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

542. 01 Matrix 所有点,用dfs

279. Perfect Squares dp

529. Minesweeper 扫雷 DFS BFS无疑了

[代码风格] :

class Cell {
int x, y, h;
Cell (int xx, int yy, int hh) {
x = xx;
y = yy;
h = hh;
}
} class CellComparator implements Comparator<Cell> {
public int compare(Cell a, Cell b) {
if (a.h > b.h) {
return 1;
}else if (a.h == b.h) {
return 0;
}else {
return -1;
}
}
} public class Solution {
/**
* @param heights: a matrix of integers
* @return: an integer
*/
public int trapRainWater(int[][] heightMap) {
int result = 0;
int n = heightMap.length;
int m = heightMap[0].length;
int[][] visit = new int[n][m];
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
//corner case
if (heightMap == null || m == 0 || n == 0) {
return 0;
}
PriorityQueue<Cell> q = new PriorityQueue<>(new CellComparator());
//get 4 edges into q
for (int i = 0; i < n; i++) {
q.offer(new Cell(i, 0, heightMap[i][0]));
q.offer(new Cell(i, m - 1, heightMap[i][m - 1]));
visit[i][0] = 1;
visit[i][m - 1] = 1;
}
for (int j = 0; j < m; j++) {
q.offer(new Cell(0, j, heightMap[0][j]));
q.offer(new Cell(n - 1, j, heightMap[n - 1][j]));
visit[0][j] = 1;
visit[n - 1][j] = 1;
}
//bfs
while (!q.isEmpty()) {
Cell now = q.poll();
int cx = now.x;
int cy = now.y; for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < m &&
visit[nx][ny] == 0) {
visit[nx][ny] = 1;
q.offer(new Cell(nx, ny, Math.max(now.h, heightMap[nx][ny])));
result += Math.max(0, now.h - heightMap[nx][ny]);
}
}
} return result;
}
}

接雨水12 · Trapping Rain Water12的更多相关文章

  1. [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II

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

  2. LeetCode 42. 接雨水(Trapping Rain Water)

    题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况 ...

  3. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

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

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

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

  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 收集雨水 II

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

  7. [LintCode] Trapping Rain Water 收集雨水

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

  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] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

随机推荐

  1. ubuntu16.04 tensorflow pip 方式安装

    首先,需要知道   tensorflow  1.5版本以上包括 1.5版本  的GPU类型都是需要安装  cuda9.0的,  tensorflow-gpu  1.4版本是可以使用cuda 8.0. ...

  2. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  3. UWP 轻量级样式定义(Lightweight Styling)

    在 UWP 中,可以通过给空间直接设置属性或在 Style 中设置属性来定制空间的样式:不过这样的样式定义十分有限,比如按钮按下时的样式就没法儿设置.当然可以通过修改 Template 来设置控件的样 ...

  4. 【liunx】nslookup命令

    “nslookup”域名解析是什么? 假设我们要开个网站,首先我们要去提供域名申请的机构申请域名,然后绑定一个IP地址, 域名比较容易记忆,不像IP地址都是数字,申请完域名,绑定域名,DNS就写入域名 ...

  5. vue+webpack多个项目共用组件动态打包单个项目

    原文复制:https://www.jianshu.com/p/fa19a07b1496 修改了一些东西,因为sh脚本不能再window电脑执行,所以改成了node脚本.这是基于vue-cli2.0配置 ...

  6. streamsets 包管理

    streamsets 自带一个包管理,可以方便的进行三方组件的添加,比如我们需要处理mongodb 数据,默认是没有添加这个组件的,操作如下: 选择包管理 选择组件 安装 点击安装 提示界面 安装完成 ...

  7. p/Invoke工具

    开源的工具 下面这个链接来下载这个工具: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a06085 ...

  8. web应用中Filter过滤器之开发应用

    1 过滤器的简单开发应用部署 首先讲解过滤器的开发部署运行基本流程,在这里先通过一个简单的示例: 1)编写过滤器类 编写一个简单的过滤器类:SimpleFilter,实现Filter接口,完整的代码为 ...

  9. Python 迭代对象、迭代器、生成器

    原文出处: liuzhijun 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Generators,俺写的这篇文章是按照自己的理解做的参考翻译,算不上是原文 ...

  10. php变量的实现

    1.php变量的实现 变量名 zval ,变量值 zend_value,php7的变量内存管理的引用计数 在zend_value结构上,变量的操作也都是zend_value实现的. //zend_ty ...