Skip to content

Instantly share code, notes, and snippets.

View priyankvex's full-sized avatar
💭
Chilling 🍺

Priyank Verma priyankvex

💭
Chilling 🍺
View GitHub Profile
@priyankvex
priyankvex / complete_the_itinerary.py
Created November 2, 2019 09:36
Complete the itinerary
from collections import defaultdict
class Solution(object):
def solve(self, trips):
# create a sorted adjacency list from the trips
graph = defaultdict(list)
for source, destination in trips:
@priyankvex
priyankvex / 8_puzzle_solver.py
Created November 1, 2019 13:44
8 puzzle solver
import copy
import heapq
class Node(object):
def __init__(self, cost, moves, board, parent, blank_tile):
self.cost = cost
self.moves = moves
self.board = board
@priyankvex
priyankvex / minimum_path_in_atmost_k_steps.py
Created October 24, 2019 16:12
Minimum cost path in at most K steps
class Solution(object):
def solve(self, graph, steps, source, destination):
min_cost = 9999999999999999
min_path = None
queue = [(source, 0, [source])]
visited = {source: 0}
while queue:
current_city = queue[0]
@priyankvex
priyankvex / range_sum_bst.py
Created October 19, 2019 12:49
Range sum in BST
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# Store the range of values
self.range = None
# Store the sum of the tree planted at this node
self.sum = None
@priyankvex
priyankvex / word_ladder.py
Last active October 19, 2019 10:40
Shortest word ladder
class Solution(object):
def isAdjacent(self, first, second):
count = 0
for i, ch in enumerate(first):
if first[i] != second[i]:
count += 1
@priyankvex
priyankvex / min_stack.py
Created October 12, 2019 12:45
Min stack
class MinStack(object):
def __init__(self):
self.main_stack = []
self.min_stack = []
def push(self, n):
if not self.main_stack:
self.main_stack.append(n)
self.min_stack.append(n)
@priyankvex
priyankvex / minimum_cost_to_connect_all_cities.py
Created October 12, 2019 09:52
Minimum cost to connect all the cities
class Solution(object):
def solve(self, cities_graph, n):
# We have to find the minimum spanning tree cost for the given graph.
# MST will give us the minimum cost to connect all the cities
# store the nodes that are in the MST
mst_nodes = set()
# store the minimum explored cost for each node
node_cost = {0: 0}
@priyankvex
priyankvex / find_peak_element.py
Created September 18, 2019 15:43
Find the peak element
class Solution:
def findPeakElement(self, a):
low = 0
high = len(a) - 1
ans = self.helper(a, low, high)
return ans
def helper(self, a, low, high):
@priyankvex
priyankvex / kadanes.py
Created September 13, 2019 14:23
Kadane's algorithm
class Solution(object):
def solve(self, a):
if not a:
return 0
sum_so_far = a[0]
max_sum = a[0]
@priyankvex
priyankvex / construct_the_array.py
Created September 7, 2019 10:41
Construct the array
class Solution(object):
def solve(self, n, k, x):
a = [0] * n
b = [0] * n
if x == 1:
a[0] = 1
else: