Skip to content

Instantly share code, notes, and snippets.

View jhidajat's full-sized avatar
🧠

Jeremy Hidajat jhidajat

🧠
View GitHub Profile
"""
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
"""
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.
"""
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.
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:
'''
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):
'''
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
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
@jhidajat
jhidajat / forest.py
Last active September 25, 2018 22:56
'''
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 = []
'''
ORIGINAL ANSWER
'''
from collections import defaultdict
from sys import maxsize
class Solution:
def maxVacationDays(self, flights, days):
'''
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