首页 > 代码库 > LeetCode "Unique Paths"
LeetCode "Unique Paths"
Now I believe thoughts leading to DP is brutal DFS.. DFS is brutal force enumeration, but that‘s too much and naive. We need pruning. So, DFS + Pruning leads to DP! Equation is simple in this case. 1A!
class Solution {public: #define MAXN 101 int uniquePaths(int m, int n) { // Initial thought was DFS, which is brutal force. // So try pruning.. DFS + Pruning means DP int A[MAXN][MAXN]; for (int i = 0; i <= m; i++) A[i][0] = 0; for (int i = 0; i <= n; i++) A[0][i] = 0; A[1][1] = 1; for (int i = 1; i <= m; i ++) for (int j = 1; j <= n; j ++) { if (i == 1 && j == 1) continue; A[i][j] = A[i - 1][j] + A[i][j - 1]; } return A[m][n]; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。