Skip to content

Instantly share code, notes, and snippets.

View vetional's full-sized avatar

Prateek Shrivastava vetional

  • Amazon
  • Hydrabad
  • 02:41 (UTC +05:30)
View GitHub Profile
@vetional
vetional / main.py
Created October 8, 2017 03:07
caterpillar created by vetional - https://repl.it/MPFx/1
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
@vetional
vetional / main.py
Created October 8, 2017 04:00
Max profit 1D wine problem (DP) created by vetional - https://repl.it/MQlJ/0
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:
@vetional
vetional / main.py
Created October 8, 2017 10:31
Number of paths, min_cost path via 2D DP created by vetional - https://repl.it/MQuc/3
# 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
@vetional
vetional / main.py
Last active October 8, 2017 17:40
String problem created by vetional - https://repl.it/MRdh/1
# 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)
@vetional
vetional / main.py
Created October 9, 2017 03:22
3sum problem created by vetional - https://repl.it/MS4f/0
# 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
@vetional
vetional / main.py
Created October 9, 2017 08:10
Kth largest in BST created by vetional - https://repl.it/MSfG/2
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:
@vetional
vetional / balanced parentheses.py
Created November 21, 2017 07:27
balanced parentheses created by vetional - https://repl.it/@vetional/balanced-parentheses
# 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:
@vetional
vetional / mysql-pandas-import.py
Created April 8, 2018 15:08 — forked from stefanthoss/mysql-pandas-import.py
Import data from a MySQL database table into a Pandas DataFrame using the pymysql package.
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()