forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66fe0d4
commit 73a6bde
Showing
4 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
02_Dynamic-Programming/12. DP Using 1D Array/05. Jump Game III.py
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
02_Dynamic-Programming/12. DP Using 1D Array/05. Unique Binary Search Trees.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# https://leetcode.com/problems/unique-binary-search-trees/ | ||
# https://youtu.be/H1qjjkm3P3c | ||
class Solution: | ||
def numTrees(self, n: int) -> int: | ||
''' | ||
# Recursive Solution | ||
def solve(n): | ||
if n <= 1: return 1 | ||
if n == 2: return 2 | ||
if n == 3: return 5 | ||
ans = 0 | ||
for i in range(n): | ||
ans += solve(i) * solve(n-i-1) | ||
return ans | ||
return solve(n) | ||
''' | ||
# Dynamic Programming | ||
dp = [0] * (n+1) | ||
dp[0] = 1 | ||
|
||
for i in range(1, n+1): | ||
for j in range(i): | ||
dp[i] += dp[j] * dp[i-j-1] | ||
|
||
return dp[-1] | ||
|
||
# Time: O(n*n) | ||
# Space: O(n) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# https://leetcode.com/problems/jump-game-iii/ | ||
|
||
# -------------------------- DFS Approach ------------------------------- | ||
class Solution: | ||
def canReach(self, arr, start): | ||
seen = set() | ||
|
||
def dfs(n): | ||
if n in seen: return False | ||
if not 0 <= n < len(arr): return False | ||
if arr[n] == 0: return True | ||
seen.add(n) | ||
return dfs(n - arr[n]) or dfs(n + arr[n]) | ||
|
||
return dfs(start) | ||
|
||
# Time O(N); as traversing evry element only once | ||
# Space: O(N) | ||
|
||
|
||
# -------------------------- BFS Approach ------------------------------- | ||
class Solution: | ||
def canReach(self, arr, start): | ||
q = [start] | ||
visited = [False]*len(arr) | ||
|
||
while q: | ||
tmp = q.pop() | ||
if arr[tmp] == 0: return True | ||
visited[tmp] = True | ||
r, l = tmp + arr[tmp], tmp - arr[tmp] | ||
if r < len(arr) and visited[r] == False: | ||
q.append(r) | ||
if l >= 0 and visited[l] == False: | ||
q.append(l) | ||
|
||
return False | ||
|
||
# Time O(N); as traversing evry element only once | ||
# Space: O(N) |