- 浏览: 183683 次
- 性别:
- 来自: 济南
文章分类
最新评论
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.
Note: m and n will be at most 100.
这道题目是Unique Paths的变形题目,用1代表障碍物,有障碍物的地方就是死路,同样采用动态规划的思想,只是多了一些限定条件,当为1时,我们就把当前点的值设定为0;当为0的时候,如果此时(i = 0或者j = 0)这时当前点的值等于它前面的点的值(相当于初始化的过程),如果此时(i > 0并且j > 0),动态转移方程为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。代码如下:
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.
Note: m and n will be at most 100.
这道题目是Unique Paths的变形题目,用1代表障碍物,有障碍物的地方就是死路,同样采用动态规划的思想,只是多了一些限定条件,当为1时,我们就把当前点的值设定为0;当为0的时候,如果此时(i = 0或者j = 0)这时当前点的值等于它前面的点的值(相当于初始化的过程),如果此时(i > 0并且j > 0),动态转移方程为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。代码如下:
public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { if(obstacleGrid == null || obstacleGrid.length == 0) return 0; int m = obstacleGrid.length; int n = obstacleGrid[0].length; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) { if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0; else if (i == 0 && j == 0) obstacleGrid[0][0] = 1; else if(i == 0) obstacleGrid[0][j] = obstacleGrid[0][j - 1]; else if(j == 0) obstacleGrid[i][0] = obstacleGrid[i - 1][0]; else obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]; } return obstacleGrid[m - 1][n - 1]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 499Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 706For a undirected graph with tre ...
相关推荐
javascript js_leetcode题解之63-unique-paths-ii.js
c语言入门 C语言_leetcode题解之63-unique-paths-ii.c
Unique Paths Unique Paths II Longest Palindromic Substring Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II...
44. Unique Paths II:与 Unique Paths 类似,但是考虑网格中障碍物的处理。 45. Maximum Sum Subarray:找出一个数字序列中和最大的连续子序列。 46. Maximum Product Subarray:找出一个数字序列中乘积最大的连续...
javascript js_leetcode题解之62-unique-paths.js
44. Unique Paths II:不同路径数的计算,其中包含障碍物。 45. Maximum Sum Subarray:计算子数组的最大和。 46. Maximum Product Subarray:计算子数组的最大乘积。 47. Coins in a Line:动态规划解决博弈问题,...
- "不同路径"(Unique Paths)和"不同路径 II"(Unique Paths II)同样是关于路径计算的动态规划问题。 **二分搜索(Binary Search)** 二分搜索算法适用于有序数组或列表的快速查找问题。 - "搜索插入位置"...
c语言入门 C语言_leetcode题解之62-unique-paths.c
62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/03/09 70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 ...
- Unique Paths / Unique Paths II: 前者计算从矩阵的左上角到右下角的路径数量;后者则考虑了障碍物。 - Minimum Path Sum: 在一个m×n的网格中,找到从左上角到右下角的路径,使得路径上的数字总和最小。 - Plus ...
Unique Paths II **知识点:** - **问题描述:** - 同 62 题,但网格中存在障碍物。 - **解决方案分析:** - **动态规划:** - 定义 `dp[i][j]` 表示到达 `(i, j)` 的路径数量。 - 如果当前位置有障碍物,则...
1、记忆化 2、二维dp 3、滚动优化
II mod之后,可能数学公式就不能简单地给出答案了。但对我来说,其实和前一题没区别。动态规划处理这种问题,早就是牛刀杀鸡了。。 Single Number 碰巧我知道异或的解法。如果不知道的话,想想还是有点费事的。 ...
63.Unique Paths II 64.最小路径和 70.爬楼梯 72.编辑距离 122. 买卖股票的最佳时机 II 152.最大积子阵列 198.强盗 322.硬币变化 贪心算法 45.跳跃游戏II 55.跳跃游戏 哈希 1.二和 3.最长不重复字符的子串 49.组字谜...
【描述】"leetcode63" 提到的这个问题,全称为“Unique Paths II”,是LeetCode中的一个经典问题。在这个问题中,任务是计算一个机器人从左上角到达右下角有多少种不同的路径。与标准的“Unique Paths”问题相比,...
- **Probe Instrumentation Phase:** In this phase, a unique path ID is assigned to each path. This allows for the identification and tracking of individual paths during program execution. - **Backwalk ...
unique_paths.py(类似于 chess_board.c!)+ unique_paths2.py chess_board.c 效率测试 bin_search.py 素数.py 零知识.py Leetcode:现在很多小方案都是leetcode,我主要是为了好玩。 著名算法:Knuth-Morris-Pratt...
public static int uniquePaths(int rows, int cols, int[][] grid) { int[][] dp = new int[rows][cols]; // 初始化第一列全部为1,表示只有一条路径到达每个点 for (int i = 0; i ; i++) { if (grid[i][0] =...