leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0062.cpp (650B)
0 class Solution { 1 public: 2 int uniquePaths(int n, int m) { 3 vector<vector<int>> mat(n, vector<int>(m, 0)); 4 queue<pair<int, int>> q; 5 q.push({n - 1, m - 1}), mat[n - 1][m - 1] = 1; 6 while (!q.empty()) { 7 auto [x, y] = q.front(); 8 q.pop(); 9 if (x - 1 >= 0) { 10 mat[x - 1][y] += mat[x][y]; 11 if (mat[x - 1][y] == mat[x][y]) q.push({x - 1, y}); 12 } 13 if (y - 1 >= 0) { 14 mat[x][y - 1] += mat[x][y]; 15 if (mat[x][y - 1] == mat[x][y]) q.push({x, y - 1}); 16 } 17 } 18 return mat[0][0]; 19 } 20 };