forked from srinidh-007/Coding_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique-path.cpp
More file actions
51 lines (38 loc) · 927 Bytes
/
unique-path.cpp
File metadata and controls
51 lines (38 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
// PROBLEM LINK: https://leetcode.com/problems/unique-paths/
int uniquePaths(int m, int n) {
vector< vector<int>>dp(m,vector<int>(n));
for(int i = 0;i<m;i++){
dp[i][n-1] = 1;
}
for(int j = 0;j<n;j++){
dp[m-1][j] = 1;
}
for(int i = m-2;i>=0;i--){
for(int j = n-2;j>=0;j--){
dp[i][j]= dp[i+1][j] + dp[i][j+1];
}
}
return dp[0][0];
}
int main(){
int n;
int m;
n = 3;
n = 2;
//unique paths hsould be 3
//From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
//1. Right -> Down -> Down
//2. Down -> Down -> Right
//3. Down -> Right -> Down
cout<<uniquePaths(n,m);
/*
Input: m = 7, n = 3
Output: 28
Input: m = 3, n = 3
Output: 6
Input: m = 3, n = 7
Output: 28
*/
}