Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List


class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
m, n = len(obstacleGrid), len(obstacleGrid[0])
if obstacleGrid[0][0] == 1 or obstacleGrid[m - 1][n - 1] == 1:
return 0
dp = [0] * n
dp[0] = 1
for row in range(m):
for col in range(n):
if obstacleGrid[row][col] == 1:
dp[col] = 0
elif col > 0:
dp[col] += dp[col - 1]
return dp[n - 1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
const m = obstacleGrid.length;
const n = obstacleGrid[0].length;
if (obstacleGrid[0][0] === 1 || obstacleGrid[m - 1][n - 1] === 1) return 0;
const dp: number[] = new Array(n).fill(0);
dp[0] = 1;
for (let row = 0; row < m; row++) {
for (let col = 0; col < n; col++) {
if (obstacleGrid[row][col] === 1) {
dp[col] = 0;
} else if (col > 0) {
dp[col] += dp[col - 1];
}
}
}
return dp[n - 1];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_139_unique_paths_ii import Solution


class UniquePathsIITestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_example_1(self):
self.assertEqual(2, self.solution.uniquePathsWithObstacles([[0,0,0],[0,1,0],[0,0,0]]))

def test_example_2(self):
self.assertEqual(1, self.solution.uniquePathsWithObstacles([[0,1],[0,0]]))

def test_obstacle_at_start(self):
self.assertEqual(0, self.solution.uniquePathsWithObstacles([[1,0],[0,0]]))

def test_obstacle_at_end(self):
self.assertEqual(0, self.solution.uniquePathsWithObstacles([[0,0],[0,1]]))

def test_single_cell_no_obstacle(self):
self.assertEqual(1, self.solution.uniquePathsWithObstacles([[0]]))

def test_single_cell_obstacle(self):
self.assertEqual(0, self.solution.uniquePathsWithObstacles([[1]]))


if __name__ == '__main__':
unittest.main()
Loading