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 / SumBetweenNums.py
Last active September 22, 2021 18:57
Formula to calculate sum between two numbers
# Reference : https://www.quora.com/How-do-you-calculate-the-sum-of-the-numbers-between-x-and-y
# This formula does't depened on which one of a or b is greater
def sumBetweenNums(a,b):
m = abs(a-b+1)
k = a+b
r = (m*k)/2
return r
# OR
@ssshukla26
ssshukla26 / RemoveKMinAmplitude.py
Created September 19, 2021 21:57
Remove K consecutive elements from array, so that amplitude of remaining elements is minimal.
# Reference : https://stackoverflow.com/questions/69236733/remove-n-consecutive-elements-from-array-so-that-amplitude-of-remaining-element/69237657#69237657
# remove K consecutive elements from array, so that amplitude of remaining elements is minimal
from itertools import accumulate as acc
from math import inf
A = [3,5,1,3,9,8]
B = A[::-1]
K = 3
N = len(A)
@ssshukla26
ssshukla26 / EditDistance.py
Created September 18, 2021 18:10
Edit Distance [LeetCode 72]
# Reference : https://www.youtube.com/watch?v=AuYujVj646Q
class Solution:
def minDistance(self, x: str, y: str) -> int:
# Init
n = len(x)
m = len(y)
@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 October 17, 2021 04:32
[Pseudocode] Union By Rank and Find With Path Compression
# Init
parent = dict()
rank = dict()
# Find With Path Compression
def find(node):
nonlocal parent
# Find/Update the parent of the node
@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