This file contains hidden or 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 caterpillarMethod(A, s): | |
n = len(A) | |
front, total = 0, 0 | |
for back in xrange(n): | |
print "back: ",back | |
while (front < n and total + A[front] <= s): | |
total += A[front] | |
front += 1 | |
print "total:", total, "back:", back, "front:",front |
This file contains hidden or 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 maxprofit(price, year, memo): | |
print price, year | |
profit = 0 | |
if len(price) == 1: | |
return price[0] * year | |
key = '-'.join([str(p) for p in price]) + '--' +str(year) | |
if key in memo: |
This file contains hidden or 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
# Problem Statement : Given a cost matrix Cost[][] where Cost[i][j] denotes the Cost | |
# of visiting cell with coordinates (i,j), find a min-cost path to reach a cell (x,y) | |
# from cell (0,0) under the condition that you can only travel one step right or one | |
# step down. (We assume that all costs are positive integers) | |
def paths(Cost, row, col, dr, dc, memo): | |
if row == dr and col == dc: | |
return 1 |
This file contains hidden or 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
# Given a string, find the longest substring without repeating characters. | |
from collections import defaultdict | |
def solution(s): | |
if not s: | |
return None | |
tmp = "" | |
longest = "" | |
dict = defaultdict(int) |
This file contains hidden or 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
# Determine if any 3 integers in an array sum to 0. | |
# For example, for array [4, 3, -1, 2, -2, 10], the function should return true since 3 + (-1) + (-2) = 0. To make things simple, each number can be used at most once. | |
def solution(A): | |
A.sort() # O(nlogn) | |
print A | |
for i, x in enumerate(A): # O(n**2) | |
start = 0 |
This file contains hidden or 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: | |
def __init__(self, data): | |
print "creating node: ", data | |
self.data = data | |
self.left = None | |
self.right = None | |
def insert(self, data): | |
if self.data > data: |
This file contains hidden or 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
# Print all possible n pairs of balanced parentheses. | |
# For example, when n is 2, the function should print “(())” and “()()”. | |
# When n is 3, we should get “((()))”, “(()())”, “(())()”, “()(())”, “()()()”. | |
def fn(l,r): | |
#print ('call : fn(' + str(l) + ',' + str(r) + ')') | |
if l < 0 or r < 0 or l > r: | |
return 0 | |
if l == 0 and r == 0: |
This file contains hidden or 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 pandas as pd | |
import pymysql | |
from sqlalchemy import create_engine | |
engine = create_engine('mysql+pymysql://<user>:<password>@<host>[:<port>]/<dbname>') | |
df = pd.read_sql_query('SELECT * FROM table', engine) | |
df.head() |
OlderNewer