ARTS是什么?
Algorithm:每周至少作一个leetcode的算法题;
Review:阅读并点评至少一篇英文技术文章;
Tip:学习至少一个技术技巧;
Share:分享一篇有观点和思考的技术文章。java
LeetCode 407. Trapping Rain Water IInode
题目思路分析
算法
在 1 个 2 维的矩阵中,每一个格子都有其高度,问这个 2 维矩阵可以盛多少的水。首先咱们分析,格子可以盛水的必要条件是其周围存在格子比当前格子高,这样水才可以被框得住,可是仔细一想,最外围的格子怎么办?它们是存不了水的,能够把最外围的格子想象成围栏,它们的做用就是保证里面格子的水不会流出来,因此咱们就得先考虑这些格子,它们的高度直接决定了内部格子的蓄水量,可是这些格子也有局部性,一个格子的长短并不会影响矩阵当中全部的格子,可是它会影响与其相邻的格子,那么咱们就须要有一个考虑的顺序,那就是优先考虑最外层最短的格子,因为每一个格子都会影响到其周围的格子,内部格子也须要列入考虑范围,每次咱们都考虑最短的格子,而后看其周围有没有没考虑过的比它还短的格子,因而就有了考虑的前后顺序:数据库
这里须要注意的是,每次归入考虑范围的格子是加了水以后的高度,而不是以前的高度,缘由想一下应该不难理解。另外就是可使用了 “堆” 这个数据结构来帮助实现寻找 “当前考虑范围内最短的格子” 这个操做步骤。api
参考代码缓存
private class Pair {
int x, y, h;
Pair(int x, int y, int h) {
this.x = x;
this.y = y;
this.h = h;
}
}
private int[] dirX = {0, 0, -1, 1};
private int[] dirY = {-1, 1, 0, 0};
public int trapRainWater(int[][] heightMap) {
if (heightMap.length == 0 || heightMap[0].length == 0) {
return 0;
}
int m = heightMap.length;
int n = heightMap[0].length;
PriorityQueue<Pair> pq = new PriorityQueue<>(new Comparator<Pair>() {
@Override
public int compare(Pair a, Pair b) {
return a.h - b.h;
}
});
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < n; ++i) {
pq.offer(new Pair(0, i, heightMap[0][i]));
pq.offer(new Pair(m - 1, i, heightMap[m - 1][i]));
visited[0][i] = true;
visited[m - 1][i] = true;
}
for (int i = 1; i < m - 1; ++i) {
pq.offer(new Pair(i, 0, heightMap[i][0]));
pq.offer(new Pair(i, n - 1, heightMap[i][n - 1]));
visited[i][0] = true;
visited[i][n - 1] = true;
}
int result = 0;
while (!pq.isEmpty()) {
Pair cur = pq.poll();
for (int k = 0; k < 4; ++k) {
int curX = cur.x + dirX[k];
int curY = cur.y + dirY[k];
if (curX < 0 || curY < 0 || curX >= m || curY >= n || visited[curX][curY]) {
continue;
}
if (heightMap[curX][curY] < cur.h) {
result += cur.h - heightMap[curX][curY];
}
pq.offer(new Pair(curX, curY,
Math.max(heightMap[curX][curY], cur.h)));
visited[curX][curY] = true;
}
}
return result;
}
复制代码
一篇关于 Node.js 中项目代码结构的文章:
安全
Bulletproof node.js project architecture服务器
做者的有几个观点我以为仍是很值得借鉴的:数据结构
做者同时在文章的开头给出了他以为不错的一个文件结构:
src
app.js # App entry point
api # Express route controllers for all the endpoints of the app
config # Environment variables and configuration related stuff
jobs # Jobs definitions for agenda.js
loaders # Split the startup process into modules
models # Database models
services # All the business logic is here
subscribers # Event handlers for async task
types # Type declaration files (d.ts) for Typescriptapp
最近在学 Python,总结一些列表、元组、集合、字典的使用注意事项:
列表和元组
集合和字典
此次继续来积累算法知识,此次看看深度优先搜索,经过它,咱们能够发现不少高级的算法,将学过的东西创建联系、融会贯通也是一件很是有意义的事情。