-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_paths.cpp
More file actions
38 lines (32 loc) 路 993 Bytes
/
Copy pathunique_paths.cpp
File metadata and controls
38 lines (32 loc) 路 993 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
// https://leetcode.com/problems/unique-paths/
class Solution {
public:
int uniquePaths(int m, int n) {
if(m == 1 && n == 1) return 1;
vector<vector<int>> matrix( m, vector<int> (n) );
for(int i = 0; i < m; i++) {
matrix[i][n-1] = 1;
}
for(int i = 0; i < n; i++) {
matrix[m-1][i] = 1;
}
matrix[m-1][n-1] = 0;
// PrintMatrix(matrix);
for(int i = m-2; i >= 0; i--){
for(int j = n-2; j >=0; j--) {
matrix[i][j] += matrix[i+1][j] + matrix[i][j+1];
}
}
// PrintMatrix(matrix);
return matrix[0][0];
}
void PrintMatrix(vector<vector<int>> &matrix) {
for(int i = 0; i < matrix.size(); i++) {
cout << "| ";
for(int j = 0; j < matrix[0].size(); j++) {
cout << matrix[i][j] << " | ";
}
cout << endl;
}
}
};