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 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: |
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
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 |
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(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] |
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 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 |
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(object): | |
def isAdjacent(self, first, second): | |
count = 0 | |
for i, ch in enumerate(first): | |
if first[i] != second[i]: | |
count += 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
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) |
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(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} |
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 findPeakElement(self, a): | |
low = 0 | |
high = len(a) - 1 | |
ans = self.helper(a, low, high) | |
return ans | |
def helper(self, a, low, high): |
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(object): | |
def solve(self, a): | |
if not a: | |
return 0 | |
sum_so_far = a[0] | |
max_sum = a[0] |
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(object): | |
def solve(self, n, k, x): | |
a = [0] * n | |
b = [0] * n | |
if x == 1: | |
a[0] = 1 | |
else: |
NewerOlder