Skip to content

Commit

Permalink
leetcode #62 and #63 unique paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kpbochenek committed Dec 11, 2016
1 parent dd09549 commit 0e0abd0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | Medium | [link](./search_insert_position.cpp) |
| 46 | [Permutations](https://leetcode.com/problems/permutations/) | Medium | [link](./permutations.cpp) |
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | Medium | [link](./maximum_subarray.cpp) |
| | | | |
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | Medium | [link](./unique_paths.cpp) |
| 63 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | Medium | [link](./unique_paths_2.cpp) |



51 changes: 51 additions & 0 deletions leetcode/unique_paths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Author: Krzysztof Bochenek
// Email: kpbochenek@gmail.com
// --------------------------------
#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <stack>
#include <math.h>
#include <algorithm>
#include <string>
#include <iostream>

// ---------------------

typedef long long int ll;
typedef unsigned long long ull;

using namespace std;

// ---------------------

template<class T>
void print_vec(const vector<T> &v, string desc = "") {
cout << desc << " ";
for (T e: v) {
cout << e << " ";
}
cout << endl;
}

// ---------------------

class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
for (int i=0; i<m; ++i) {
for (int j=0; j<n; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}
};
58 changes: 58 additions & 0 deletions leetcode/unique_paths_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Author: Krzysztof Bochenek
// Email: kpbochenek@gmail.com
// --------------------------------
#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <stack>
#include <math.h>
#include <algorithm>
#include <string>
#include <iostream>

// ---------------------

typedef long long int ll;
typedef unsigned long long ull;

using namespace std;

// ---------------------

template<class T>
void print_vec(const vector<T> &v, string desc = "") {
cout << desc << " ";
for (T e: v) {
cout << e << " ";
}
cout << endl;
}

// ---------------------

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();

int dp[m][n];
for (int i=0; i<m; ++i) {
for (int j=0; j<n; ++j) {
if (obstacleGrid[i][j] == 1) {
dp[i][j] = 0;
} else {
if (i == 0 && j == 0) { dp[i][j] = 1; }
else if (i == 0) { dp[i][j] = dp[i][j-1]; }
else if (j == 0) { dp[i][j] = dp[i-1][j]; }
else { dp[i][j] = dp[i-1][j] + dp[i][j-1]; }
}
}
}
return dp[m-1][n-1];
}
};

0 comments on commit 0e0abd0

Please sign in to comment.