LeetCode417. Pacific Atlantic Water Flow

2023-12-02 11:28

本文主要是介绍LeetCode417. Pacific Atlantic Water Flow,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is less than or equal to the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

Example 1:

Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:

Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.

Constraints:

m == heights.length
n == heights[r].length
1 <= m, n <= 200
0 <= heights[r][c] <= 105

二、题解

class Solution {
public:int dirs[4][2] = {1,0,0,1,-1,0,0,-1};void dfs(vector<vector<int>>& heights,vector<vector<bool>>& visited,int x,int y){visited[x][y] = true;for(int i = 0;i < 4;i++){int nextX = x + dirs[i][0];int nextY = y + dirs[i][1];if(nextX < 0 || nextX >= heights.size() || nextY < 0 || nextY >= heights[0].size()) continue;if(heights[nextX][nextY] >= heights[x][y] && !visited[nextX][nextY]){dfs(heights,visited,nextX,nextY);}}}vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {int m = heights.size(),n = heights[0].size();vector<vector<bool>> pacificVisited(m,vector<bool>(n,false));vector<vector<bool>> atlanticVisited(m,vector<bool>(n,false));//标记左右for(int i = 0;i < m;i++){dfs(heights,pacificVisited,i,0);dfs(heights,atlanticVisited,i,n-1);}//标记上下for(int j = 0;j < n;j++){dfs(heights,pacificVisited,0,j);dfs(heights,atlanticVisited,m-1,j);}vector<vector<int>> res;//统计结果for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(pacificVisited[i][j] && atlanticVisited[i][j]){res.push_back({i,j});}}}return res;}
};

这篇关于LeetCode417. Pacific Atlantic Water Flow的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/445158

相关文章

GNSS CTS GNSS Start and Location Flow of Android15

目录 1. 本文概述2.CTS 测试3.Gnss Flow3.1 Gnss Start Flow3.2 Gnss Location Output Flow 1. 本文概述 本来是为了做Android 14 Gnss CTS 的相关环境的搭建和测试,然后在测试中遇到了一些问题,去寻找CTS源码(/cts/tests/tests/location/src/android/locat

Understanding the GitHub Flow

这里看下Github的入门介绍    --链接 GitHub Flow is a lightweight, branch-based workflow that supports teams and projects where deployments are made regularly. This guide explains how and why GitHub Flow works

Versioned Staged Flow-Sensitive Pointer Analysis

VSFS 1.Introduction2.Approach2.1.相关概念2.2.VSFS 3.Evaluation参考文献 1.Introduction 上一篇blog我介绍了目前flow-sensitive pointer analysis常用的SFS算法。相比IFDS-based方法,SFS显著通过稀疏分析提升了效率,但是其内部依旧有许多冗余计算,留下了很大优化空间。 以

LeetCode - 11. Container With Most Water

11. Container With Most Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个N条垂直于x轴的直线,从中找两条直线和x轴组成一个桶状容器,使得这个容器的容量最大. analyse:

LeetCode - 42. Trapping Rain Water

42. Trapping Rain Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体. analyse: 首先找出最高的积木,然后从前往后一直

Salt Function Flow:深度解析复杂网关编排的优势与实践

系列文章索引: Salt Function Flow 系列文章 在业务流程编排中,处理条件逻辑、并行任务、以及复杂的流程分支是常见的挑战。对于需要高度灵活性和扩展性的项目,Salt Function Flow 提供了强大的网关编排能力,使开发者能够轻松定义和管理复杂的业务流程。本文将深入探讨Salt Function Flow中的复杂网关编排功能,展示其如何通过排他网关、并行执行等功能应对复杂的

Salt Function Flow 系列文章

Salt Function Flow 是一款Java开发、轻量级、内存级的业务流程编排框架,旨在帮助开发者通过函数式编程的方式定义和管理复杂的业务流程。它以高效、灵活的流程处理为核心,适用于多种业务场景,从简单任务自动化到复杂业务逻辑处理。 系列文章: Salt Function Flow:深度研发经验的沉淀,打造轻量级高效流程编排框架 Salt Function Flow:深度解析复杂网关编排

Kotlin 流 Flow

挂起函数可以异步地返回一个值,而对于返回多个值,可以使用流,使用 emit(x) 发射多个值, collect { } 来收集值。 默认 流是冷的,只有 收集(collect) 时才会执行。 1. 流的创建 flow {} 生成流,emit(x) 来发射值;xxx.asFlow() 集合转成Flow;flowOf(1, 2, 3) 生成固定值的流。 1.1 flow {} flow {}

CSS-标准文档流(Normal Flow)

目录 1 定义2 脱离文档流3 相对定位文章参考 1 定义 文档流中:内联元素默认从左到右流,遇到阻碍或者宽度不够自动换行,继续按照从左到右的方式布局。块级元素单独占据一行,并按照从上到下的方式布局。 2 脱离文档流 文档一旦脱离文档流,则元素不再按照文档流的排列方式进行排列,如块级元素脱离文档流后,该块级元素不再接着上个元素从上到下排列,而是成为第一个元素,从顶部开始排列

Container With Most Water (Java实现)

当看见这道题时想到的一个答案就是暴力破解,附上代码 <span style="font-size:18px;"><span style="font-size:18px;">package com.alibaba;import java.util.Scanner;public class Solution{public int maxArea(int[] height){int maxCapac