Skip to content

Commit 612e17e

Browse files
author
wuduhren
committed
update
1 parent b68839f commit 612e17e

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

problems/python3/car-fleet.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
3+
N = len(position)
4+
5+
timeInfo = []
6+
for i in range(N):
7+
timeInfo.append((position[i], (target-position[i])/speed[i]))
8+
timeInfo.sort(reverse=True)
9+
10+
stack = []
11+
for _, time in timeInfo:
12+
stack.append(time)
13+
if len(stack)>=2 and stack[-2]>=stack[-1]:
14+
stack.pop()
15+
16+
return len(stack)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
3+
ans = [0]*len(temperatures)
4+
stack = []
5+
6+
for i, temp in enumerate(temperatures):
7+
while stack and stack[-1][0]<temp:
8+
prevTemp, prevIndex = stack.pop()
9+
ans[prevIndex] = i-prevIndex
10+
stack.append((temp, i))
11+
12+
return ans
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def evalRPN(self, tokens: List[str]) -> int:
3+
operators = set(['+', '-', '*', '/'])
4+
stack = []
5+
6+
for c in tokens:
7+
if c in operators:
8+
n2 = stack.pop()
9+
n1 = stack.pop()
10+
11+
if c=='+':
12+
stack.append(n1+n2)
13+
elif c=='-':
14+
stack.append(n1-n2)
15+
elif c=='*':
16+
stack.append(n1*n2)
17+
elif c=='/':
18+
stack.append(int(n1/n2))
19+
else:
20+
stack.append(int(c))
21+
22+
return stack.pop()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def generateParenthesis(self, n: int) -> List[str]:
3+
def helper(parentheses, left, right):
4+
if len(parentheses)==n*2:
5+
ans.append("".join(parentheses))
6+
return
7+
8+
if left<n:
9+
parentheses.append('(')
10+
helper(parentheses, left+1, right)
11+
parentheses.pop()
12+
13+
if left>right:
14+
parentheses.append(')')
15+
helper(parentheses, left, right+1)
16+
parentheses.pop()
17+
18+
ans = []
19+
helper([], 0, 0)
20+
return ans

problems/python3/min-stack.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#"stack" is just a normal stack.
2+
#"minStack" stores the min of the stack element in the same position.
3+
#i.e. At the time stack[x] put in to the stack. minStack[x] is the min.
4+
5+
class MinStack:
6+
7+
def __init__(self):
8+
self.stack = []
9+
self.minStack = []
10+
11+
def push(self, val: int) -> None:
12+
self.stack.append(val)
13+
self.minStack.append(val if (not self.minStack or val<self.minStack[-1]) else self.minStack[-1])
14+
15+
def pop(self) -> None:
16+
self.minStack.pop()
17+
return self.stack.pop()
18+
19+
def top(self) -> int:
20+
return self.stack[-1]
21+
22+
def getMin(self) -> int:
23+
return self.minStack[-1]

0 commit comments

Comments
 (0)