LeetCode之动态规划

62. Unique Paths

QuestionEditorial Solution

Total Accepted: 86710 Total Submissions: 239084 Difficulty: Medium

A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

How many possible unique paths are there?

使用动态规划来求解本题,在每个节点处,只有两个选择,向下或者向右走。本节点可由上一个节点向下或者向右走过来,两种方法:

1)使用一个二位数组count[i][j]来表示从起始点到达坐标(i,j)的,有两种,(i-1,j)-->(i,j)及(i,j-1)-->(i,j),即count[i][j] = count[i-1][j]+count[i][j-1],此时时间复杂度为O(mn),空间复杂度为O(mn)

public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] count = new int[m+1][n+1];
        for(int i=0;i<=m;i++){
            for(int j=0;j<=n;j++){
                if(i==0||j==0){
                    count[i][j] = 0;
                }else if(i==1&&j==1){
                    count[i][j]=1;
                }else{
                    count[i][j] = count[i-1][j]+count[i][j-1];
                }
            }
        }
        return count[m][n];
    }
}

2)使用一个数组count[i]来表示从起始点到达某一行某一列的路径数量,此时有,count[i] = count[i-1]+count[i],此时,时间复杂度为O(mn),空间复杂度为O(n):

//因为只需求得最终结果,而不需要知道到达每一个坐标的路径数量public class Solution {
    public int uniquePaths(int m, int n) {
        int[] count = new int[n];
        count[0]=1;
        for(int i=0;i<m;i++){
            for(int j=1;j<n;j++){
                count[i] = count[i-1]+count[i];
            }
        }
        return count[n-1];
    }
}

63. Unique Paths II

QuestionEditorial Solution

Total Accepted: 65258 Total Submissions: 222092 Difficulty: Medium

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.

时间: 2024-10-10 09:46:39

LeetCode之动态规划的相关文章

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

【Leetcode】动态规划问题详解(持续更新)

1.动态规划算法步骤(Dynamic Programming) 动态规划算法一般用来求解最优化问题,当问题有很多可行解,而题目要求寻找这些解当中的"最大值"/"最小值"时,通常可以采用DP. 动态规划算法与分治法相似,都是通过组合子问题的解来求解原问题.所不同的是,动态规划应用于子问题重叠的情况,在递归求解子问题的时候,一些子子问题可能是相同的,这种情况下,分治法会反复地计算同样的子问题,而动态规划对于相同的子问题只计算一次. 动态规划算法的设计步骤: 1.刻画最优

LeetCode之“动态规划”:Unique Binary Search Trees &amp;&amp; Unique Binary Search Trees II

1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题目分析参考自一博

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e

LeetCode之“动态规划”:Dungeon Game

题目链接 题目要求: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must f

LeetCode之“动态规划”:Maximum Subarray

题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If yo

[LeetCode] HouseRobber 动态规划

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi

LeetCode之“动态规划”:Distinct Subsequences

题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing th