From 55d91e186aa13959e1e3ef1be093b3f3b3312919 Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Tue, 26 Jul 2022 13:57:08 +0530 Subject: [PATCH 1/4] added all paths from src to target --- ...code 797 - all paths from source to target.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 14. Questions/leetcode 797 - all paths from source to target.py 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 From 60dc69b147efbaf037e14ba3d2da6520c5f71396 Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Tue, 26 Jul 2022 13:57:23 +0530 Subject: [PATCH 2/4] added find town judge --- .../leetcode 997 - find the town judge.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 14. Questions/leetcode 997 - find the town judge.py 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 + From f9d63fccccd265be48ee7a2a6edfc9fcf728be3e Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Thu, 28 Jul 2022 13:05:49 +0530 Subject: [PATCH 3/4] added min abs diff in bst --- ...30 - minimum absolute difference in BST.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 14. Questions/leetcode 530 - minimum absolute difference in BST.py 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 + + From 93a29b36499427109a0a0c85238e3e9f2579551b Mon Sep 17 00:00:00 2001 From: mihirs16 Date: Thu, 28 Jul 2022 13:59:17 +0530 Subject: [PATCH 4/4] added unique binary search trees --- ...eetcode 96 - unique binary search trees.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 14. Questions/leetcode 96 - unique binary search trees.py 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] +