Skip to content

Instantly share code, notes, and snippets.

View willwangcc's full-sized avatar
⚙️
workflow

Will willwangcc

⚙️
workflow
View GitHub Profile
# Time: O(N) where N is the number of keys in the input dictionary
# Space: O(N) since the ouput dict is asymptotically as big as the input dict
def flatten_dictionary(dictionary):
pass # your code goes here
flat_dict = {}
flat_dict_helper("", dictionary, flat_dict)
return flat_dict
def flat_dict_helper(inital_key, dictionary, flat_dict):
# Time: O(N log(N))
# Space: O(1)
'''
[2, 100, 50, 120, 1000] newBudget = 190
1000, 120, 100, 50, 2, 0
120, 120, 100, 50, 2, 0
100, 100, 100, 50, 2, 0
50, 50, 50, 50, 2, 0
2, 2, 2, 2, 2, 0
# 349. Intersection of Two Arrays
# Time: O(n)
# Space: O(n)
'''
i
[1, 1, 2, 2, 3, 5]
[2, 2, 5]
j
'''
# Time: O(E+nlogn), where E is the length of flights
# Space: O(n), the size of the heap
# Reference: https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
'''
# Dijkstra's algorithm
If we continually extend our potential flightpaths in order of cost, we know once we've reached the destination dst that it was the lowest cost way to get there.
'''
import collections
class Solution(object):
# Time: O(E∗K), where E is the length of flights.
# Space: O(n), the space used to store cur and pre
# Reference: https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
# Bellman-ford algorithm
class Solution(object):
def findCheapestPrice(self, n, flights, src, dst, K):
"""
:type n: int
class Solution(object):
def racecar(self, target):
"""
:type target: int
:rtype: int
"""
curstep = set([])
curstep.add((0,1))
allstep = set([])
allstep.add((0,1))
# Time: O(n)
# Space: O(n)
# Before vs After
#####################################
## Before
#####################################
@willwangcc
willwangcc / 809.expressive-words.py
Created April 1, 2018 08:22
elegant code: itertools.groupby(S); 2. zip(count, count2)
# Time: O(n)
# Space: O(n)
# Before vs After
#####################################
## Before
#####################################
@willwangcc
willwangcc / 810.chalkboard-xor-game.py
Created April 1, 2018 08:11
elegant code: reduce()?
# Time: O(n)
# Space: O(n)
# Before vs After
#####################################
## After
#####################################
@willwangcc
willwangcc / 811.subdomain-visit-count.py
Last active April 1, 2018 07:54
Elegant code: 1. string.join() 2. "{} {}".format(x, y)
# Time: O(n)
# Space: O(n)
# Before vs After
#####################################
## After
#####################################