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.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 |
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://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) |
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=AuYujVj646Q | |
class Solution: | |
def minDistance(self, x: str, y: str) -> int: | |
# Init | |
n = len(x) | |
m = len(y) | |
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
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] | |
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=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 |
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
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]: |
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 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 |
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
# 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 |
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
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 |