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
""" | |
A trade is defined as a string containing the 4 properties given below separated by commas: | |
- Symbol (4 alphabetical characters, left-padded by spaces) | |
- Side (either "B" or "S" for buy or sell) | |
- Quantity (4 digits, left-padded by zeros) | |
- ID (6 alphanumeric characters) | |
e.g. "AAPL,B,0100,ABC123" represents a trade of a buy of 100 shares of AAPL with ID "ABC123" | |
Given two lists of trades - called "house" and "street" trades, write code to create groups |
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
""" | |
Take in a stream of orders (as lists of limit price, quantity, and side, like ["155", "3", "buy"]) | |
and return the total number of executed shares. | |
Rules: | |
- An incoming buy is compatible with a standing sell if the buy's price is >= the sell's price. | |
Similarly, an incoming sell is compatible with a standing buy if the sell's price <= the buy's price. | |
- An incoming order will execute as many shares as possible against the best compatible order, | |
if there is one (by "best" we mean most favorable to the incoming order). | |
- Any remaining shares will continue executing against the best compatible order, if there is one. |
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
""" | |
String转换: | |
看两个String, String a和String b之间能否互换,可以有两种变换方式: | |
1)String里面任意的两个字符swap | |
2) String里面某种字符全部换成另一种字符 | |
input: String a, String b | Output: boolean | |
eg: String a: abbzccc String b: babzzcz -> True | |
等差数列: | |
给定两个Array A, B. A里面可以添加任意B中元素,返回A可以构造的最长等差数列的长度,如果不能构成等差数列,返回-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
def findAnagram(s, pattern): | |
if not s or not pattern: | |
return [] | |
cp = Counter(pattern) | |
anagrams = set([]) | |
for c in s[:m]: | |
if c in cp: | |
cp[c] -= 1 | |
tot = sum(cp.val()) | |
if tot == 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
''' | |
TIME: O(mn) m is average length of a word in dictionary, n number of words in a dictionary. I.E O(n) where n is the total characters in a dictionary | |
SPACE: O(1) | |
''' | |
## DIFF1 | |
# TIME: O( min(l1,l2) ) | |
# SPACE: O(1) | |
def diff1(w1,w2): |
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
''' | |
Assuming stream.next() and stream.hasNext() is O(1): | |
Time: O(n^2) due to while loop and filter, and DONE loop | |
Space: O(n) | |
for n total chars in target list | |
''' | |
from collections import defaultdict |
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 sys import maxsize | |
MAX_INT, MIN_INT = maxsize, -maxsize | |
def fixBST(root): | |
def _dfs(node, upperbound, lowerbound): | |
if not node: return | |
l, r = node.left.val, node.right.val | |
if l >= node.val or l <= lowerbound: | |
node.left = 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
''' | |
RUNTIME: O(n) | |
SPACE: O(1) since we only convert deleted list to set | |
where n is number of nodes | |
''' | |
def forest(root, to_delete): | |
to_delete = set(to_delete) | |
new_roots = [] | |
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
''' | |
ORIGINAL ANSWER | |
''' | |
from collections import defaultdict | |
from sys import maxsize | |
class Solution: | |
def maxVacationDays(self, flights, days): | |
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
''' | |
RUNTIME: O(2^n) | |
SPACE: O(n) | |
for n # of cells in grid | |
''' | |
from sys import maxsize | |
def police(grid): | |
m, n = len(grid), len(grid[0]) | |
#build new grid |