diff --git a/14. Questions/leetcode 530 - minimum absolute difference in BST.py b/14. Questions/leetcode 530 - minimum absolute difference in BST.py new file mode 100644 index 0000000..481fc30 --- /dev/null +++ b/14. Questions/leetcode 530 - minimum absolute difference in BST.py @@ -0,0 +1,32 @@ +# minimum absolute difference in BST | leetcode 530 | https://leetcode.com/problems/minimum-absolute-difference-in-bst/ +# method: dfs, inorder traversal + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def getMinimumDifference(self, root: TreeNode): + minDiff = float('inf') + prevNod = None + + def dfs(node): + nonlocal minDiff, prevNod + if node is None: + return + + dfs(node.left) + + if prevNod != None: + minDiff = min(minDiff, abs(node.val - prevNod)) + prevNod = node.val + + dfs(node.right) + + dfs(root) + return minDiff + + diff --git a/14. Questions/leetcode 797 - all paths from source to target.py b/14. Questions/leetcode 797 - all paths from source to target.py new file mode 100644 index 0000000..199a51d --- /dev/null +++ b/14. Questions/leetcode 797 - all paths from source to target.py @@ -0,0 +1,16 @@ +# all paths from source to target | leetcode 797 | https://leetcode.com/problems/all-paths-from-source-to-target/ +# method: dfs + +class Solution: + def allPathsSourceTarget(self, graph: list[list[int]]) -> list[list[int]]: + possiblePaths = [] + + def dfs(node, visited): + if node == len(graph) - 1: + possiblePaths.append(visited) + + for neighbour in graph[node]: + dfs(neighbour, [*visited, node]) + + dfs(0, [0]) + return possiblePaths diff --git a/14. Questions/leetcode 96 - unique binary search trees.py b/14. Questions/leetcode 96 - unique binary search trees.py new file mode 100644 index 0000000..23b1287 --- /dev/null +++ b/14. Questions/leetcode 96 - unique binary search trees.py @@ -0,0 +1,21 @@ +# unique binary search trees | leetcode 96 | https://leetcode.com/problems/unique-binary-search-trees/ +# method: dp, use cached results for subtrees of all possible roots + +class Solution: + def numTrees(self, n: int) -> int: + # cache of possible trees + possibleTrees = [1] * (n + 1) + + # for each number of nodes + for numNodes in range(2, n + 1): + + # for each possible root + possibleSubTrees = 0 + for possibleRoot in range(1, numNodes + 1): + Left = possibleRoot - 1 + Right = numNodes - possibleRoot + possibleSubTrees += possibleTrees[Left] * possibleTrees[Right] + possibleTrees[numNodes] = possibleSubTrees + + return possibleTrees[n] + diff --git a/14. Questions/leetcode 997 - find the town judge.py b/14. Questions/leetcode 997 - find the town judge.py new file mode 100644 index 0000000..c22c04d --- /dev/null +++ b/14. Questions/leetcode 997 - find the town judge.py @@ -0,0 +1,21 @@ +# find the town judge | leetcode 997 | https://leetcode.com/problems/find-the-town-judge/submissions/ +# method: decrement trust value if you trust someone, increment if someone trusts you + +class Solution: + def findJudge(self, n: int, trust: list[list[int]]) -> int: + + # for each person + # trust += 1 if someone trusts you + # trust -= 1 if you trust someone + trustValue = [0] * (n + 1) + + for edge in trust: + trustValue[edge[0]] -= 1 + trustValue[edge[1]] += 1 + + for i in range(1, n + 1): + if trustValue[i] == (n - 1): + return i + + return -1 +