LeetCode--不同路径(动态规划)
不同路径
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
问总共有多少条不同的路径?
示例 1:
输入: m = 3, n = 2
输出: 3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
1. 向右 -> 向右 -> 向下
2. 向右 -> 向下 -> 向右
3. 向下 -> 向右 -> 向右
示例 2:
输入: m = 7, n = 3
输出: 28
排列组合(代码待修改以及优化)
因为机器到底右下角,向下几步,向右几步都是固定的,
比如,m=3, n=2,我们只要向下 1 步,向右 2 步就一定能到达终点。
m*n的棋盘,一共需要走(m-1)+(n-1)步,向右走m-1步,向下走n-1步,这(m-1)+(n-1)步中,只要确定了哪些步向右,即同时确定了哪些步向下走,反之亦然。
答案即C(m+n-2,m-1)或C(m+n-2,n-1)
[code]public static int uniquePaths(int m, int n) { int[] pre = new int ; int[] cur = new int ; Arrays.fill(pre, 1); Arrays.fill(cur,1); for (int i = 1; i < m;i++){ for (int j = 1; j < n; j++){ cur[j] = cur[j-1] + pre[j]; } pre = cur.clone(); } return pre[n-1]; }
代码2
[code]#include <iostream> using namespace std; class Solution { public: int uniquePaths(int m, int n) { double res = 1; for (int i = 1; i <= n - 1; i++) { res *= (double(m + i - 1) / double(i)); } return (int)round(res); } };
Array.fill()
用法1:接受2个参数
Arrays.fill( a1, value );
boolean[] a1 = new boolean[5];
Arrays.fill( a1,true );
结果 a1[] = {true,true,true,true,true};
用法2:接受4个参数
String[] a9 = new String[6];
Arrays.fill(a9, "Hello");
Arrays.fill(a9, 3, 5,"World");
结果是 a9[] = {Hello,Hello,Hello,World,World,Hello};
clone()
clone() 方法实现数组拷贝
动态规划
我们令 dp[i][j] 是到达 i, j 最多路径
动态方程:dp[i][j] = dp[i-1][j] + dp[i][j-1]
注意,对于第一行 dp[0][j],或者第一列 dp[i][0],由于都是在边界,所以只能为 1
时间复杂度:O(m*n)O(m∗n)
空间复杂度:O(m * n)O(m∗n)
优化:因为我们每次只需要 dp[i-1][j],dp[i][j-1]
[code]public static void main(String[] args) { System.out.println("清湖输入m和n的值"); Scanner sc = new Scanner(System.in); String str = sc.nextLine().trim(); List<String> digitList = new ArrayList<>(); Pattern p = Pattern.compile("[^0-9]"); Matcher m = p.matcher(str); String result = m.replaceAll(""); String[] c = new String[result.length()+1]; for (int i = 0; i < result.length(); i++) { c[i] = result.substring(i, i+1); } System.out.println(uniquePaths(Integer.parseInt(c[0]),Integer.parseInt(c[1]))); } public int uniquePaths(int m, int n) { int[][] dp = new int[m] ; for (int i = 0; i < n; i++) dp[0][i] = 1; for (int i = 0; i < m; i++) dp[i][0] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1]; } 进行优化*************************************************** class Solution { public int uniquePaths(int m, int n) { int[] cur = new int ; Arrays.fill(cur,1); for (int i = 1; i < m;i++){ for (int j = 1; j < n; j++){ cur[j] += cur[j-1] ; } } return cur[n-1]; }
- Leetcode 062 不同路径 Python (动态规划)
- LeetCode62. 不同路径 动态规划 python3实现
- LeetCode刷题——动态规划,No.62不同路径
- LeetCode --- 62. 不同路径
- leetcode 62. 不同路径
- 动态规划——不同的路径
- leetcode 62.不同路径
- Leetcode-62.不同路径
- [LeetCode 63] 不同路径 II
- leetcode62. 不同路径(dp)
- 算法题目总结 -- 不同路径( leetcode64题)
- LeetCode-115.不同的子序列(相关话题:动态规划)
- Leetcode 62&63&64 不同路径 Java
- (力扣---动态规划)不同路径
- LeetCode 63. Unique Path II(所有不同路径之二)
- [LeetCode]62 不同的路径总数
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
- leetcode 63. 不同路径 II dp
- LeetCode62/63. 不同路径问题(python)
- leetcode 120:三角形最小路径和(动态规划)