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 / MaxFromAnIndex.py
Last active September 15, 2021 20:57
Get Max to the left of an array, this can also be used to get max to the right of an array
nums = [0,1,0,2,1,0,1,3,2,1,2,1]
n = len(nums)
# Function to get max to the left
# of each index of an array
def maxToLeft(arr):
# Nothing left for first index
maxLeft = [-1]
@ssshukla26
ssshukla26 / MaxProdSubArr.py
Created September 14, 2021 04:59
Maximum Product Subarray [LeetCode 152]
# Reference : https://www.youtube.com/watch?v=IOMjN6r7ju8
class Solution:
def maxProduct(self, nums: List[int]) -> int:
# NOTE: The crux of the solution is
# to look at the array from both the ends.
# That is in forward and in reverse order.
# Also one more important concept is
# anything multiplied to 0 will loose
@ssshukla26
ssshukla26 / MonotonicStack.py
Last active September 14, 2021 02:27
Monotonic Stacks
nums = [1,4,3,2,2,1,9]
m = len(nums)
# Next Smallest
next_smallest = [-1] * m
stack = []
for i in range(m):
if stack:
while stack and stack[-1][1] > nums[i]:
@ssshukla26
ssshukla26 / ClassicKadane.py
Last active September 25, 2021 00:10
Kadane's Algo, this works with -ve elements also
from math import inf
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
curr_sum = -inf
curr_max = -inf
for num in nums:
curr_sum = max(curr_sum + num, num)
curr_max = max(curr_max, curr_sum)
return curr_max
@ssshukla26
ssshukla26 / PathFromRootToTarget.py
Created September 9, 2021 05:23
Function to return path from root to a target node in a binary tree
# A function which returns path from root to the given target
def getPath(curr, target, path: List):
# if current node is not none
if curr:
# Add current node to the path
path.append(curr)
# if current node is target return path
@ssshukla26
ssshukla26 / UnionByRank-FindWithPathCompression.py
Last active September 3, 2025 05:18
[Pseudocode] Union By Rank and Find With Path Compression
# Class for union find with path compression
class UnionFind:
def __init__(self, n):
self.n = n
self.roots = [i for i in range(n)]
self.ranks = [0 for _ in range(n)]
return
# find with path compression
def find(self, x) -> int:
@ssshukla26
ssshukla26 / CanonicalPath.py
Created September 7, 2021 15:51
Given a string path, which is an absolute path to a file or directory in a Unix-style file system, convert it to the simplified canonical path. [LeetCode 71]
class Solution:
def simplifyPath(self, path: str) -> str:
# Reconstuct path to suit the logic
# The curx is to maintain "/" at the end
# of the path, and the logic revolve around
# it.
# Add "/" to last if not already there
@ssshukla26
ssshukla26 / Divison.py
Last active September 9, 2021 16:20
Division without using division, multiplication or mod operator [Leetcode 29]
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
# -2147483648 through 2147483647
MIN_VAL = -2147483648
MAX_VAL = 2147483647
# If divisor is 1 or -1
if abs(divisor) == 1:
@ssshukla26
ssshukla26 / MaxEvents.py
Last active September 5, 2021 20:42
Maximum Number of Events That Can Be Attended [LeetCode 1353]
# Reference : https://www.youtube.com/watch?v=EKZhEN9P2-I
import heapq
class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
# Sort the events in ascending order of their start time
events.sort(key = lambda x: x[0])
@ssshukla26
ssshukla26 / Trie.py
Last active September 26, 2021 18:18
Prefix Tree (Trie) [LeetCode 208] [LeetCode 1268]
from typing import List
class TrieNode:
def __init__(self):
self.next = dict() # pointer to next node, starting with a key char
self.end = False # by default false
class Trie:
def __init__(self):