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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Codec: | |
def serialize(self, root): |
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
# Function to find prime factors of a number | |
def primeFactors(num): | |
# Function to reduce num by a factor | |
def reduceByFactor(factor): | |
nonlocal num | |
while num % factor == 0: | |
num = num // factor | |
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
# 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 deleteAndReplaceWithMin(self, root: TreeNode): | |
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
# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: |
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
import re | |
class Solution: | |
def validIPAddress(self, queryIP: str) -> str: | |
def is_ipv4(ip: str) -> bool: | |
ip = ip.split(".") | |
if len(ip) == 4: # must have 4 different parts |
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 sortedcontainers import SortedDict | |
class Element: | |
def __init__(self, v): | |
self.v = v # Current value | |
self.p = None # Previous pointer | |
self.n = None # Next Pointer | |
return | |
class MaxStack: |
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
# Init | |
parent = dict() | |
rank = dict() | |
# Find With Path Compression | |
def find(node): | |
nonlocal parent | |
# Find/Update the parent of the node |
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=Jn4mbZVkeik | |
from collections import OrderedDict | |
# A class to hold the value and the counter of the key | |
class KeyNode(): | |
def __init__(self, val, cnt): | |
self.val = val | |
self.cnt = cnt |
NewerOlder