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
# Find Strongly Connected Components in a Graph using Kosaraju Algo | |
# Reference : https://www.youtube.com/watch?v=Rs6DXyWpWrI | |
from typing import List, Set | |
class Solution(): | |
def reverseEdges(self, edges: List[List[int]]) -> List: | |
edges_rev = [list() for _ in range(len(edges))] |
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
# Reference : https://www.youtube.com/watch?v=ZeDNSeilf-Y | |
from typing import List | |
class Solution(): | |
def __init__(self) -> None: | |
self.timer = 0 | |
self.discovery = [] | |
self.low = [] |
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
# Targan Alog: https://www.youtube.com/watch?v=ZeDNSeilf-Y | |
# Find Bridges: https://www.youtube.com/watch?v=Rhxs4k6DyMM&t=0s | |
# Reference: https://gist.github.com/SuryaPratapK/2774cb957a27448b485609418e272f2b | |
# Find Bridges using Targan Algo | |
from typing import List, Set, Dict | |
class Solution(): | |
def __init__(self) -> None: | |
self.timer = 0 |
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
""" | |
References: | |
https://www.youtube.com/watch?v=JZBQLXgSGfs | |
https://www.youtube.com/watch?v=kaBX2s3pYO4 | |
""" | |
class Solution: | |
# The purpose of find with path compression is to reduce the | |
# no. of hopes needed to determine the parent node. This reduction | |
# will help to decrease overall runtime from O(n) to O(log n) |
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
# Reference : https://www.youtube.com/watch?v=Sj5Z-jaE2x0 | |
from typing import List, Set, Dict | |
class Solution: | |
def findMin(self, start_node: int, is_processed: set, spg: List, graph: Dict) -> int: | |
node = start_node | |
value = float("inf") |
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
from collections import OrderedDict | |
class LRUCache: | |
def __init__(self, capacity: int): | |
self.__capacity = capacity | |
self.__cache = OrderedDict() | |
return | |
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
# Find pivot in a sorted but shifted/rotated array using binary search | |
def pivot(arr, low, high): | |
if low <= high: | |
# Calc Mid | |
mid = low + (high-low)//2 | |
# Base Case: monotonic sequence e.g. [0,1,2,3] | |
if arr[low] < arr[high]: |
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
class Solution: | |
def calculate(self, s: str) -> int: | |
# Strip and replace all the spaces | |
s = s.strip().replace(" ","") | |
# Evaluate | |
def update(stack: List, curr: str, operator: str): | |
if curr: |
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
from matplotlib import pyplot as plt | |
import numpy as np | |
m = [ | |
[1,1,0,1,0,0,1,1,0,0], | |
[0,1,1,1,1,1,1,1,0,0], | |
[0,0,1,0,1,1,0,0,0,0], | |
[0,0,0,1,1,0,1,1,0,0], | |
[0,0,0,1,0,0,1,0,1,0], | |
[0,0,0,0,1,1,1,1,1,1], |
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
from collections import defaultdict, deque | |
class Solution: | |
def __init__(self): | |
self.n = 0 | |
self.islands = defaultdict(lambda: set()) | |
return | |
def withinBoundary(self, node: Tuple) -> bool: |
OlderNewer