Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3464565

Browse files
Merge pull request #15 from mihirs16/master
added leetcode questions
2 parents d233c9c + 93a29b3 commit 3464565

4 files changed

+90
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# minimum absolute difference in BST | leetcode 530 | https://leetcode.com/problems/minimum-absolute-difference-in-bst/
2+
# method: dfs, inorder traversal
3+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
class Solution:
12+
def getMinimumDifference(self, root: TreeNode):
13+
minDiff = float('inf')
14+
prevNod = None
15+
16+
def dfs(node):
17+
nonlocal minDiff, prevNod
18+
if node is None:
19+
return
20+
21+
dfs(node.left)
22+
23+
if prevNod != None:
24+
minDiff = min(minDiff, abs(node.val - prevNod))
25+
prevNod = node.val
26+
27+
dfs(node.right)
28+
29+
dfs(root)
30+
return minDiff
31+
32+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# all paths from source to target | leetcode 797 | https://leetcode.com/problems/all-paths-from-source-to-target/
2+
# method: dfs
3+
4+
class Solution:
5+
def allPathsSourceTarget(self, graph: list[list[int]]) -> list[list[int]]:
6+
possiblePaths = []
7+
8+
def dfs(node, visited):
9+
if node == len(graph) - 1:
10+
possiblePaths.append(visited)
11+
12+
for neighbour in graph[node]:
13+
dfs(neighbour, [*visited, node])
14+
15+
dfs(0, [0])
16+
return possiblePaths
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# unique binary search trees | leetcode 96 | https://leetcode.com/problems/unique-binary-search-trees/
2+
# method: dp, use cached results for subtrees of all possible roots
3+
4+
class Solution:
5+
def numTrees(self, n: int) -> int:
6+
# cache of possible trees
7+
possibleTrees = [1] * (n + 1)
8+
9+
# for each number of nodes
10+
for numNodes in range(2, n + 1):
11+
12+
# for each possible root
13+
possibleSubTrees = 0
14+
for possibleRoot in range(1, numNodes + 1):
15+
Left = possibleRoot - 1
16+
Right = numNodes - possibleRoot
17+
possibleSubTrees += possibleTrees[Left] * possibleTrees[Right]
18+
possibleTrees[numNodes] = possibleSubTrees
19+
20+
return possibleTrees[n]
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# find the town judge | leetcode 997 | https://leetcode.com/problems/find-the-town-judge/submissions/
2+
# method: decrement trust value if you trust someone, increment if someone trusts you
3+
4+
class Solution:
5+
def findJudge(self, n: int, trust: list[list[int]]) -> int:
6+
7+
# for each person
8+
# trust += 1 if someone trusts you
9+
# trust -= 1 if you trust someone
10+
trustValue = [0] * (n + 1)
11+
12+
for edge in trust:
13+
trustValue[edge[0]] -= 1
14+
trustValue[edge[1]] += 1
15+
16+
for i in range(1, n + 1):
17+
if trustValue[i] == (n - 1):
18+
return i
19+
20+
return -1
21+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /