Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Wate…
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { public: vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) { return {};…
Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Wate…
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目描述 Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pa…
正常做的,用了645MS..感觉DFS的时候剪枝有问题.. 为了剪枝可能需要标记一个点的4种情况: 1:滨临大西洋,所有太平洋来的点可以通过: 2:濒临太平洋,所有大西洋来的点可以通过: 3:都不濒临,直接RETURN: 4:都濒临,通过: 这样的话用DP记录情况,应该能做.. 看了答案发现一开始思路就错了..但是这个645MS居然能AC.......... 正确思路看第二段代码吧. public class Solution { public List<int[]> pacificAtlan…
Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Wate…
Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Wate…
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要大的点,但是我写反了..可以先对左边的队列进行BFS,保存其visited,再接着对右边的队列进行BFS,当访问到之前已经访问过的结点时,则加入到结果中. 实现 // // #include "../PreLoad.h" /* Given the following 5x5 matrix:…
417. 太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高到低或者在同等高度上流动. 请找出那些水流既可以流动到"太平洋",又能流动到"大西洋"的陆地单元的坐标. 提示: 输出坐标的顺序不重要 m 和 n 都小于150 示例: 给定下面的 5x5 矩阵:…
太平洋大西洋水流问题 给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度."太平洋"处于大陆的左边界和上边界,而"大西洋"处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高到低或者在同等高度上流动. 请找出那些水流既可以流动到"太平洋",又能流动到"大西洋"的陆地单元的坐标. 提示: 输出坐标的顺序不重要 m 和 n 都小于150 示例: 给定下面的 5x5 矩阵: 返回: […
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度.“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界. 规定水流只能按照上.下.左.右四个方向流动,且只能从高到低或者在同等高度上流动. 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标. 提示: 输出坐标的顺序不重要m 和 n 都小于150 示例: 给定下面的 5x5 矩阵: 太平洋 ~ ~ ~ ~ ~ ~ 1 2 2 3 (5) * ~ 3 2 3 (4) (4) * ~ 2…
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For ex…
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],…
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存成一个元组 (val, idx) 找到最高的点(可能有多个,任取一个),记为 (now_val, now_idx). 向左找第一个val不大于now_val的点(left_val, left_idx). 以left_val作为水平面,用height[left_val+1, now_val-1]中每一…
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha…
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe…
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of li…
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: https://leetcode.com/problems/trapping-rain-water/ [标签] Array; Stack; Two Pointers 这个题我感觉很难,我自己是完全不会.下面贴的是别人想到的好方法,自己适当做了些简单调整. 我觉得,这个解法非常巧妙的使用了双向的 t…
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if th…
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCode打击一次.自“抱”自“泣”,逻辑推理能力太差了. 题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines ar…
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description   Problem: 根据所给数组的值,按照上图的示意图.求解积水最大体积.     首先对所给数组进行遍历操作,求出最大高度所对应的下标为maxIndex   之后从左向右进行遍历,设置左边高度为leftMax 并初始化为 height[0],从i==1到i==maxIndex进行遍历.不断更新water,以及leftMax     当height[…
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of…
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th…
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 我做题,一般是按照AC率排序选着做的,这道题一不留神,难度居然还是Hard,好吧,确实…
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe…
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which togeth…
42. Trapping Rain Water Problem's Link ---------------------------------------------------------------------------- Mean: 在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体. analyse: 首先找出最高的积木,然后从前往后一直扫到最高积木,从后往前一直扫到最高积木,两部分体积相加即可. Time complexity: O(N) view code ;  …
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 解题思路: 先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,J…
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a…