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,總結一些列表、元組、集合、字典的使用注意事項:
列表和元組
集合和字典
此次繼續來積累算法知識,此次看看深度優先搜索,經過它,咱們能夠發現不少高級的算法,將學過的東西創建聯繫、融會貫通也是一件很是有意義的事情。