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 / TotalOfPairsDivisibleByK.py
Created September 5, 2021 14:42
Pairs of numbers summing to a total divisible by K [LeetCode 1010]
# 1010. Pairs of Songs With Total Durations Divisible by 60
# Reference : https://www.youtube.com/watch?v=toYgBIaUdfM
from collections import defaultdict
class Solution:
# Brute Force
#def numPairsDivisibleBy60(self, time: List[int]) -> int:
@ssshukla26
ssshukla26 / 2Sum.py
Created September 3, 2021 01:06
2sum 3sum 4sum [Leetcode 1, 15, 18]
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {} # Keep track of whaterver we have see so far
for i,num in enumerate(nums): # scan array one by one
if target-num in d: # if the difference is in dictionary
return sorted([i, d[target-num]]) # return i, and dictionary value
d[num] = i # add to dictionary the current num-index pair
return []
@ssshukla26
ssshukla26 / MaxNetworkRank.py
Created September 1, 2021 23:57
Max Network Rank of a Graph [LeetCode 1615]
from collections import defaultdict
from itertools import combinations
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
# Variables
rank = 0
indegree = defaultdict(lambda: set())
nodes = [i for i in range(n)]
@ssshukla26
ssshukla26 / AngleBetweemHandsOfClock.py
Created September 1, 2021 21:22
Angle between hour and min hand of a clock [LeetCode 1344]
# Reference: https://www.youtube.com/watch?v=zKhbe9IOATY
class Solution:
def hourAngle(self, hour: int, minutes: int) -> float:
# It takes 12 hours to cover all 360 degree
# For each hours it covers 360/12 degrees, i.e. 30 degrees
# Now there between two hours 30 degrees are covered, and
# it takes 60 mins between two hours, so for each mins
# it takes 30/60 degrees between two hours, i.e., 0.5 degrees
@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:
@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 / 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 / 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 / LRUCache.py
Last active August 1, 2025 09:16
LRU Cache using OrderedDict
# Leetcode 146. LRU Cache
class Node:
def __init__(self, key: int, value: int, prev: 'Optional[Node]' = None, next: 'Optional[Node]' = None):
self.key = key
self.value = value
self.prev = prev
self.next = next
return
@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")