Skip to content

Instantly share code, notes, and snippets.

@hansrajdas
Last active July 25, 2018 15:16
Show Gist options
  • Save hansrajdas/5a15528a8ac9a063031a74bacf4cebea to your computer and use it in GitHub Desktop.
Save hansrajdas/5a15528a8ac9a063031a74bacf4cebea to your computer and use it in GitHub Desktop.
#!/usr/bin/python
def removeObstacleUtil(numRows, numColumns, lot, row, col):
if not row and not col:
return 1 + min(removeObstacleUtil(numRows, numColumns, lot, row + 1, col),
removeObstacleUtil(numRows, numColumns, lot, row, col + 1))
elif row == numRows - 1 and col == numColumns - 1:
return -1
elif row == numRows - 1:
return 1 + removeObstacleUtil(numRows, numColumns, lot, row, col + 1)
elif col == numColumns - 1:
return 1 + removeObstacleUtil(numRows, numColumns, lot, row + 1, col)
elif lot[row][col] == 9:
return 0
else:
return 1 + min(removeObstacleUtil(numRows, numColumns, lot, row + 1, col),
removeObstacleUtil(numRows, numColumns, lot, row, col + 1))
def removeObstacle(numRows, numColumns, lot):
if numRows < 1 or numColumns > 1000 or numColumns < 1:
return -1
return removeObstacleUtil(numRows, numColumns, lot, 0, 0)
m = 3
n = 3
l = [
[1, 0, 0],
[1, 0, 0],
[1, 9, 1]
]
d = removeObstacle(m, n, l)
print 'Output: ', d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment