Skip to content

Commit 9316b42

Browse files
committed
New Stack sol
1 parent cd70ecd commit 9316b42

6 files changed

+152
-0
lines changed

Math/Sum_of_Multiples.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''Given a number a and limit N. Find the sum of multiple of a upto N.'''
2+
#Simple Iterative O(n) Approach
3+
def sumUpto(a,n):
4+
sum_ = 0
5+
i = a
6+
while i <= n:
7+
sum_ += i
8+
i += a
9+
return sum_
10+
11+
print sumUpto(7,49)
12+
13+
14+
#Another O(1) Approach
15+
def sumUpto(a,n):
16+
m = n/a
17+
sum_ = m*(m+1)/2
18+
ans = a*sum_
19+
return ans
20+
21+
print sumUpto(7,49)

Stack/evaluateExpression.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''Evaluate the value of an arithmetic expression in Reverse Polish Notation.
2+
3+
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
4+
5+
'''
6+
class Solution:
7+
# @param A : list of strings
8+
# @return an integer
9+
def evalRPN(self, tokens):
10+
stack = []
11+
for t in tokens:
12+
if t not in ["+", "-", "*", "/"]:
13+
stack.append(int(t))
14+
else:
15+
r, l = stack.pop(), stack.pop()
16+
if t == "+":
17+
stack.append(l+r)
18+
elif t == "-":
19+
stack.append(l-r)
20+
elif t == "*":
21+
stack.append(l*r)
22+
else:
23+
# here take care of the case like "1/-22",
24+
# in Python 2.x, it returns -1, while in
25+
# Leetcode it should return 0
26+
if l*r < 0 and l % r != 0:
27+
stack.append(l/r+1)
28+
else:
29+
stack.append(l/r)
30+
return stack.pop()

Stack/min-stack.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
3+
'''
4+
class MinStack:
5+
stack = []
6+
minimums = []
7+
# @param x, an integer
8+
# @return an integer
9+
def push(self, x):
10+
if len(self.stack) == 0:
11+
self.stack.append(x)
12+
self.minimums.append(x)
13+
else:
14+
self.stack.append(x)
15+
if x < self.minimums[-1]:
16+
self.minimums.append(x)
17+
else:
18+
self.minimums.append(self.minimums[-1])
19+
20+
# @return nothing
21+
def pop(self):
22+
if len(self.stack) != 0:
23+
self.stack.pop()
24+
self.minimums.pop()
25+
26+
# @return an integer
27+
def top(self):
28+
if len(self.stack) == 0:
29+
return -1
30+
else:
31+
return self.stack[-1]
32+
33+
# @return an integer
34+
def getMin(self):
35+
if len(self.stack) == 0:
36+
return -1
37+
else:
38+
return self.minimums[-1]
39+
40+
def __init__(self):
41+
self.stack = []
42+
self.minimums = []

Stack/nearest-smaller-element.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
3+
Given an array, find the nearest smaller element G[i] for every element A[i] in the array such that the element has an index smaller than i.
4+
5+
6+
'''
7+
def smaller(arr):
8+
res = []
9+
stack = []
10+
for i in range(len(arr)):
11+
while len(stack) != 0 and stack[-1] >= arr[i]:
12+
stack.pop()
13+
if len(stack) == 0:
14+
res.append(-1)
15+
else:
16+
res.append(stack[-1])
17+
18+
stack.append(arr[i])
19+
return res
20+
21+
arr = [1,3,0,2,5]
22+
print smaller(arr)

Stack/redundantBraces.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
Write a program to validate if the input string has redundant braces?
3+
Return 0/1
4+
5+
0 -> NO
6+
1 -> YES
7+
'''
8+
class Solution:
9+
# @param A : string
10+
# @return an integer
11+
def braces(self, A):
12+
stack = []
13+
for el in A:
14+
if el == "(":
15+
stack.append(el)
16+
elif el == "+" or el == "-" or el == "/" or el == "*":
17+
stack.append("exp")
18+
elif el == ")":
19+
if stack[-1] == "exp":
20+
stack.pop()
21+
stack.pop()
22+
else:
23+
return 1
24+
else:
25+
return 0

Stack/simplifydirectoryPath.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''Given an absolute path for a file (Unix-style), simplify it.'''
2+
class Solution(object):
3+
def simplifyPath(self, path):
4+
places = [p for p in path.split("/") if p!="." and p!=""]
5+
stack = []
6+
for p in places:
7+
if p == "..":
8+
if len(stack) > 0:
9+
stack.pop()
10+
else:
11+
stack.append(p)
12+
return "/" + "/".join(stack)

0 commit comments

Comments
 (0)