Comments (5)
认领
from leetcode.
/**
* @param {number[][]} matrix
* @return {number[][]}
*
* 逆向模式来做
* 先统计能从太平洋反流回来的坐标
* 再统计能从大西洋反流回来的坐标
*
* 最后将两个集合中都为true的坐标输出
*/
var pacificAtlantic = function (matrix) {
if (matrix.length == 0) return [];
var m = matrix.length,
n = matrix[0].length;
var dpt = Array.from({ // 记录能流入太平洋的位置
length: m
}, () => new Array(n).fill(false));
var dpd = Array.from({ // 记录能流入大西洋的位置
length: m
}, () => new Array(n).fill(false));
// 从太平洋的起点搜索,也就是最上边一排的元素位置和左边竖排元素的位置
for (var i = 0; i < matrix.length; i++) {
for (var j = 0, len = i == 0 ? n : 1; j < len; j++) {
temp(i, j, -1, dpt);
}
}
// 大西洋
for (var i = 0; i < matrix.length; i++) {
for (var j = n - 1, len = i == m - 1 ? 0 : n - 1; j >= len; j--) {
temp(i, j, -1, dpd);
}
}
var rel = [];
for (var i = 0; i < dpt.length; i++) {
for (var j = 0; j < dpt[i].length; j++) {
if (dpt[i][j] && dpt[i][j] == dpd[i][j]) {
rel.push([i, j])
}
}
}
return rel;
function temp(x, y, preNumber, mem) {
var nowNum = matrix[x] && matrix[x][y];
if (nowNum == null) return; // 如果跨越边界,则返回
if (mem[x][y]) { // 如果此位置已经可以流入大西洋或者太平洋,则直接返回
return mem[x][y];
}
if (nowNum >= preNumber) { // 如果此位置比上一个位置的元素值大,则符合条件,然后再搜索基于它的上下左右四个位置
mem[x][y] = true;
temp(x - 1, y, nowNum, mem);
temp(x + 1, y, nowNum, mem);
temp(x, y - 1, nowNum, mem);
temp(x, y + 1, nowNum, mem);
}
}
};
执行结果:通过 显示详情
执行用时 :152 ms, 在所有 JavaScript 提交中击败了100.00%的用户
内存消耗 :41.1 MB, 在所有 JavaScript 提交中击败了100.00%的用户
from leetcode.
认领
done
from leetcode.
/** * @param {number[][]} matrix * @return {number[][]} * * 逆向模式来做 * 先统计能从太平洋反流回来的坐标 * 再统计能从大西洋反流回来的坐标 * * 最后将两个集合中都为true的坐标输出 */ var pacificAtlantic = function (matrix) { if (matrix.length == 0) return []; var m = matrix.length, n = matrix[0].length; var dpt = Array.from({ // 记录能流入太平洋的位置 length: m }, () => new Array(n).fill(false)); var dpd = Array.from({ // 记录能流入大西洋的位置 length: m }, () => new Array(n).fill(false)); // 从太平洋的起点搜索,也就是最上边一排的元素位置和左边竖排元素的位置 for (var i = 0; i < matrix.length; i++) { for (var j = 0, len = i == 0 ? n : 1; j < len; j++) { temp(i, j, -1, dpt); } } // 大西洋 for (var i = 0; i < matrix.length; i++) { for (var j = n - 1, len = i == m - 1 ? 0 : n - 1; j >= len; j--) { temp(i, j, -1, dpd); } } var rel = []; for (var i = 0; i < dpt.length; i++) { for (var j = 0; j < dpt[i].length; j++) { if (dpt[i][j] && dpt[i][j] == dpd[i][j]) { rel.push([i, j]) } } } return rel; function temp(x, y, preNumber, mem) { var nowNum = matrix[x] && matrix[x][y]; if (nowNum == null) return; // 如果跨越边界,则返回 if (mem[x][y]) { // 如果此位置已经可以流入大西洋或者太平洋,则直接返回 return mem[x][y]; } if (nowNum >= preNumber) { // 如果此位置比上一个位置的元素值大,则符合条件,然后再搜索基于它的上下左右四个位置 mem[x][y] = true; temp(x - 1, y, nowNum, mem); temp(x + 1, y, nowNum, mem); temp(x, y - 1, nowNum, mem); temp(x, y + 1, nowNum, mem); } } };执行结果:通过 显示详情
执行用时 :152 ms, 在所有 JavaScript 提交中击败了100.00%的用户
内存消耗 :41.1 MB, 在所有 JavaScript 提交中击败了100.00%的用户
是一个常规思路。非常好.
BTW, 为什么我的144ms,才击败51.25%
✔ Accepted
✔ 113/113 cases passed (144 ms)
✔ Your runtime beats 51.25 % of javascript submissions
✔ Your memory usage beats 100 % of javascript submissions (43.9 MB)
from leetcode.
我贴一下我的代码。
思路:
总体思路还是回溯,我们对能够流入太平洋的(第一行和第一列)开始进行上下左右探测。
同样我们对能够流入大西洋的(最后一行和最后一列)开始进行上下左右探测。
最后将探测结果进行合并即可。合并的条件就是当前单元既能流入太平洋又能流入大西洋。
代码:
function dfs(i, j, height, m, matrix, rows, cols) {
if (i >= rows || i < 0) return;
if (j >= cols || j < 0) return;
if (matrix[i][j] < height) return;
if (m[i][j] === true) return;
m[i][j] = true;
dfs(i + 1, j, matrix[i][j], m, matrix, rows, cols);
dfs(i - 1, j, matrix[i][j], m, matrix, rows, cols);
dfs(i, j + 1, matrix[i][j], m, matrix, rows, cols);
dfs(i, j - 1, matrix[i][j], m, matrix, rows, cols);
}
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var pacificAtlantic = function(matrix) {
const rows = matrix.length;
if (rows === 0) return [];
const cols = matrix[0].length;
const pacific = Array.from({ length: rows }, () => Array(cols).fill(false));
const atlantic = Array.from({ length: rows }, () => Array(cols).fill(false));
const res = [];
for (let i = 0; i < rows; i++) {
dfs(i, 0, 0, pacific, matrix, rows, cols);
dfs(i, cols - 1, 0, atlantic, matrix, rows, cols);
}
for (let i = 0; i < cols; i++) {
dfs(0, i, 0, pacific, matrix, rows, cols);
dfs(rows - 1, i, 0, atlantic, matrix, rows, cols);
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (pacific[i][j] === true && atlantic[i][j] === true) res.push([i, j]);
}
}
return res;
};
扩展:
如果题目改为能够流入大西洋或者太平洋,我们只需要最后合并的时候,条件改为求或即可。
from leetcode.
Related Issues (20)
- 算法学习 HOT 1
- 树专题中双色标记法后序和前序写反了 HOT 2
- leetcode/thinkings/tree.md 出错 HOT 1
- some error
- 二分查找专题,寻找最左/右插入位置算法模板错误问题 HOT 9
- possible code error in thinkings/heap.md HOT 1
- link error HOT 4
- link is not correct
- [695.最大岛屿面积,360,面试原题]【每日一题】 HOT 3
- 【专题】 反向思考 HOT 3
- 【专题】 考虑每一项对结果到的贡献
- 【专题】递推方程时间复杂度优化
- 已发布文章的代码错误 HOT 7
- Remove duplicate CPP solution and add Python solution for problem 100.same-tree
- Add OSSF Scorecard security workflow
- 题目的排版可否改一改
- 关于二分法中查找中间点索引的算式 HOT 6
- leetcode-thinkings-tree.md BFS 模版调整 HOT 3
- anki-card 中只有10道题吗?截止到2023.11 HOT 1
- 【每日一题】- 2020-xx-xx - xxx
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.