Skip to content

Instantly share code, notes, and snippets.

View theabbie's full-sized avatar
❤️
Playing With Life Since 2002

Abhishek Choudhary theabbie

❤️
Playing With Life Since 2002
View GitHub Profile
@theabbie
theabbie / codys_assignment.py
Created March 4, 2022 12:30
Dcoder Cody's Assignment Segment Tree Solution
seg = {}
def makeSeg(arr, i, j):
if (i, j) in seg:
return seg[(i, j)]
if i == j:
seg[(i, j)] = arr[i]
return arr[i]
mid = (i + j) // 2
curr = min(makeSeg(arr, i, mid), makeSeg(arr, mid + 1, j))
@theabbie
theabbie / cashback.py
Created March 4, 2022 11:54
Dcoder Cashback Question Incorrect Approach
from functools import cmp_to_key
n = int(input())
costs = []
for _ in range(n):
c, x = input().split()
costs.append((int(c), int(x)))
@theabbie
theabbie / alice_and_dictionary.py
Created March 3, 2022 06:56
Alice and Dictionary DCoder Solution
t = int(input())
def numWords(s, i, words, cache):
n = len(s)
if i >= n:
return 0
if i in cache:
return cache[i]
maxwords = 0
for j in range(i + 1, n + 1):
@theabbie
theabbie / hashmap.py
Last active January 15, 2022 14:45
Hashmap Implementation in Python
class MyHashMap:
def __init__(self):
self.size = 107
self.arr = [[] for i in range(self.size)]
def hfn(self, key):
key = ((key >> 16) ^ key) * 0x45d9f3b
key = ((key >> 16) ^ key) * 0x45d9f3b
key = (key >> 16) ^ key
@theabbie
theabbie / 01matrix.py
Created January 15, 2022 06:49
01 Matrix Leetcode using Dijkstra's Algorithm
import heapq
from collections import defaultdict
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
dist = [[0 if cell == 0 else float('inf') for cell in row] for row in mat]
heap = []
deleted = defaultdict(int)
m = len(mat)
n = len(mat[0])
@theabbie
theabbie / pascal.py
Created January 7, 2022 11:01
Pascal Triangle Python Program
n = 5
curr = [1]
for i in range(n):
print(" " * (n - i - 1), end="")
print(" ".join("{}".format(x) for x in curr))
curr = [0] + curr + [0]
curr = [curr[j] + curr[j + 1] for j in range(i + 2)]
@theabbie
theabbie / array_manipulation.py
Created January 3, 2022 10:15
Array Manipulation Hackerrank
import heapq
def arrayManipulation(n, queries):
curr = {}
for a, b, k in queries:
curr[a] = curr.get(a, 0) + k
curr[b + 1] = curr.get(b + 1, 0) - k
heap = list(curr.items())
ln = len(heap)
heapq.heapify(heap)
@theabbie
theabbie / ugly.py
Created January 1, 2022 13:51
Nth Ugly Number
def nthUglyNumber(self, n):
N = 10000
uglies = {}
uglies[1] = True
factors = [([2], 2), ([3], 3), ([5], 5)]
while len(factors) > 0:
factor, p = factors.pop(0)
uglies[p] = True
for nn in [2, 3, 5]:
np = p * nn
@theabbie
theabbie / stocks.py
Created December 30, 2021 10:38
Best time to buy and sell stocks leetcode slow solution
def maxProfit(self, prices):
valIndex = {}
maxProfit = 0
for i, price in enumerate(prices):
if price not in valIndex:
valIndex[price] = [i, i]
else:
valIndex[price][1] = i
newprices = sorted(valIndex.keys())
n = len(newprices)
@theabbie
theabbie / Jump_Game_4.py
Last active December 28, 2021 10:53
Leetcode Jump Game 4 Slow Solution
def minJumps(arr):
n = len(arr)
paths = [[0]]
visited = set()
valIndexes = {}
for i in range(n):
item = arr[i]
valIndexes[item] = valIndexes.get(item, []) + [i]
while len(paths) > 0:
path = paths.pop(0)