当前位置:网站首页>leetcode417. Pacific Atlantic current problems (medium)
leetcode417. Pacific Atlantic current problems (medium)
2022-06-11 16:15:00 【Heavy garbage】



Ideas : Two dimensional coordinates dfs
Train of thought details : If the element is searched , The complexity will be very high , Think backwards , That is, water flows upward . Search from the left and from the top , Go to == perhaps > Search your location , The place that can be reached is the square that can flow to the Pacific Ocean , The same goes for the Atlantic Ocean , The square that can be reached is ans.
class Solution {
public:
vector<vector<int>> ans;
int flag[205][205];
int xy[4][2] = {
0, 1, 0, -1, 1, 0, -1, 0};
void dfs(vector<vector<int>>& heights, int x, int y, int val) {
int n = heights.size(), m = heights[0].size();
flag[x][y] = val;
for (int i = 0; i < 4; ++i) {
int xx = x + xy[i][0];
int yy = y + xy[i][1];
if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
if (heights[xx][yy] < heights[x][y]) continue;
if (!flag[xx][yy]) dfs(heights, xx, yy, val);
if (val == 2 && flag[xx][yy] == 1) {
ans.push_back({
xx, yy});
dfs(heights, xx, yy, val);
}
}
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
int n = heights.size(), m = heights[0].size();
for (int i = 0; i < n; ++i) dfs(heights, i, 0, 1);
for (int j = 0; j < m; ++j) dfs(heights, 0, j, 1);
for (int i = 0; i < n; ++i) {
if (flag[i][m - 1] == 1) ans.push_back({
i, m - 1}); //
dfs(heights, i, m - 1, 2);
}
for (int j = 0; j < m; ++j) {
if (flag[n - 1][j] == 1) ans.push_back({
n - 1, j});
dfs(heights, n - 1, j, 2);
}
return ans;
}
};
It's easy to get wrong : The start of the search , It could be ans, To call dfs Special consideration before .
边栏推荐
- Time processing logic for the last 7 days, the last 10 days, and the last 90 days
- Db4ai: database driven AI
- jdbc调试错误,求指导
- 项目工作区创建步骤-泽众AR自动化测试工具
- leetcode463. 岛屿的周长(简单)
- The flat life of older farmers from Beijing to Holland
- Laravel listening mode
- Cloud data management will break the island of storage and the island of team
- Overview and example analysis of opengauss database performance tuning
- 如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk
猜你喜欢

How to predict SQL statement query time?

After reading the book reading methods

PyQt5 使QPlainTextEdit控件支持行号显示

收藏 | 可解释机器学习发展和常见方法!

Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress

MySQL quick start instance (no loss)

Nat Commun|語言模型可以學習複雜的分子分布

无心剑英汉双语诗001. 《春游》

Take you in-depth understanding of AGC cloud database

如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk
随机推荐
postgresql启动过程
类的 prototype 属性和__proto__属性,类原型链有两条继承路线
Easy to use GS_ Dump and GS_ Dumpall command export data
Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
Selenium-- display waiting (medium) -- detailed explanation
[从零开始学习FPGA编程-18]:快速入门篇 - 操作步骤2-6- VerilogHDL时序电路语法分析(以计数器为例)
[LeetCode每日一题] |686.重复叠加字符串匹配
This "invisible" robot may be your future colleague
Nat Common | le Modèle linguistique peut apprendre des distributions moléculaires complexes
Collection | can explain the development and common methods of machine learning!
[learn FPGA programming from scratch -18]: quick start chapter - operation steps 2-6- VerilogHDL sequential circuit syntax analysis (taking the counter as an example)
leetcode-141. Circular linked list
From repeatedly rejected manuscripts to post-90s assistant professor, Wang Hao of Rutgers University: curiosity drives me to constantly explore
Talk about data center network again
收藏 | 可解释机器学习发展和常见方法!
Nat commun | language model can learn complex molecular distribution
leetcode417. 太平洋大西洋水流问题(中等)
Production problem troubleshooting reference
MSDN download win11 method, simple and easy to operate
What happened to the frequent disconnection of the computer at home