​LeetCode刷题实战417:太平洋大西洋水流问题

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 太平洋大西洋水流问题,我们先来看题面:

https://leetcode-cn.com/problems/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.

给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。

规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。

请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。

示例

bad23be33b08a9e1d0a5f35991433909.png

解题

可以用DFS和BFS,这里用DFS

首先,我们要找的既能流进太平洋,又能流进大西洋的,所以,我们分开讨论,找可以流进太平洋的坐标,再找大西洋的坐标.

其次,我们不需要一个一个坐标判断,我们只要考虑边界就行,我拿上边界为例,上边界都可以流入太平洋,所以我们可以从上边界进行深度遍历,就是找比它水位高的坐标,都可以流到它这里.

最后,我们把这上下左右四个边界都考虑一下

class Solution {
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> result = new ArrayList<>();
        int m = matrix.length;
        if (m == 0) return result;
        int n = matrix[0].length;

        // 定义 table1、table2 存储是否流入太平洋
        boolean[][] table1 = new boolean[m][n];
        boolean[][] table2 = new boolean[m][n];

        // 能否触达上、下
        for (int i = 0; i < n; i++) {
            dfs(matrix, 0, i, matrix[0][i], table1);
            dfs(matrix, m - 1, i, matrix[m - 1][i], table2);
        }

        // 能否触达左右
        for (int i = 0; i < m; i++) {
            dfs(matrix, i, 0, matrix[i][0], table1);
            dfs(matrix, i, n - 1, matrix[i][n - 1], table2);
        }

        // 取出都能触达的点
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (table1[i][j] && table2[i][j]) {
                    List<Integer> list = new ArrayList<>();
                    list.add(i);
                    list.add(j);
                    result.add(list);
                }
            }
        }

        return result;
    }

    public void dfs(int[][] matrix, int x, int y, int pre, boolean[][] table) {
        if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length ||
                matrix[x][y] < pre || // 当前值小于上一个值
                table[x][y] // 当前值已经被标记
        ) return;

        table[x][y] = true;

        // 上下左右
        dfs(matrix, x - 1, y, matrix[x][y], table);
        dfs(matrix, x + 1, y, matrix[x][y], table);
        dfs(matrix, x, y - 1, matrix[x][y], table);
        dfs(matrix, x, y + 1, matrix[x][y], table);
    }
}

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-400题汇总,希望对你有点帮助!

LeetCode刷题实战401:二进制手表

LeetCode刷题实战402:移掉 K 位数字

LeetCode刷题实战403:青蛙过河

LeetCode刷题实战404:左叶子之和

LeetCode刷题实战405:数字转换为十六进制数

LeetCode刷题实战406:根据身高重建队列

LeetCode刷题实战407:接雨水 II

LeetCode刷题实战408:有效单词缩写

LeetCode刷题实战409:最长回文串

LeetCode刷题实战410:分割数组的最大值

LeetCode刷题实战411:最短独占单词缩写

LeetCode刷题实战412:Fizz Buzz

LeetCode刷题实战413:等差数列划分

LeetCode刷题实战414:第三大的数

LeetCode刷题实战415:字符串相加

LeetCode刷题实战416:分割等和子集

b0b8fc87c60209a93cdb51fda6eeea4d.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程IT圈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值