Skip to content

Instantly share code, notes, and snippets.

View ssshukla26's full-sized avatar
💭
Learn Apply Repeat

Sunny Shukla ssshukla26

💭
Learn Apply Repeat
View GitHub Profile
@ssshukla26
ssshukla26 / Kosaraju.py
Created August 21, 2021 19:05
Kosaraju Algorithm In Python
# 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))]
@ssshukla26
ssshukla26 / Tarjan.py
Created August 21, 2021 19:06
Tarjan Algorithm In Python
# 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 = []
@ssshukla26
ssshukla26 / FindBridges.py
Created August 22, 2021 23:18
Find Bridges Using Targan's Algorithm
# 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
@ssshukla26
ssshukla26 / MinCostMST.py
Created August 22, 2021 23:53
Krushkal's With Union-Find (path compression)
"""
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)
@ssshukla26
ssshukla26 / Dijkstra.py
Created August 23, 2021 14:54
Dijkstra's single source shortest path algorithm
# 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")
@ssshukla26
ssshukla26 / LRUCache.py
Created August 26, 2021 03:35
LRU Cache using OrderedDict
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int):
self.__capacity = capacity
self.__cache = OrderedDict()
return
@ssshukla26
ssshukla26 / PivoteInSortedShiftedArray.py
Last active October 20, 2021 16:16
Find Pivot in a sorted but shifted/rotated array in O(log n)
# 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]:
@ssshukla26
ssshukla26 / BasicCalculatorRecursion.py
Last active September 9, 2021 18:36
A Calculator which can do basic operations like " + , - , x, / " [LeetCode 224/227/772]
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:
@ssshukla26
ssshukla26 / DrawMatrix.py
Last active September 2, 2021 16:20
Draw Matrix With Text
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],
@ssshukla26
ssshukla26 / ShortestBridge.py
Last active September 1, 2021 05:06
Shortest Bridge [LeetCode 934]
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: