Skip to content

Commit 20b5a1f

Browse files
author
aaron.liu
committed
add files
1 parent 2308ad4 commit 20b5a1f

7 files changed

+156
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
'''
3+
4+
'''
5+
from typing import List
6+
import collections
7+
8+
9+
class Solution:
10+
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
11+
adjList = collections.defaultdict(list)
12+
for edge in edges:
13+
adjList[edge[0]].append(edge[1])
14+
adjList[edge[1]].append(edge[0])
15+
visited = set()
16+
return self.dfs(0, adjList, visited, hasApple) * 2
17+
18+
def dfs(self, cur_node: int, graph: dict[int, List[int]], visited: set[int], hasApple: List[bool]) -> int:
19+
step = 0
20+
for nbr in graph[cur_node]:
21+
if not nbr in visited:
22+
visited.add(cur_node)
23+
step += self.dfs(nbr, graph, visited, hasApple)
24+
visited.remove(cur_node)
25+
if (hasApple[cur_node] or step != 0) and cur_node != 0:
26+
step += 1
27+
return step
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
'''
3+
4+
'''
5+
from typing import List
6+
import collections
7+
8+
class Solution:
9+
def possiblyEquals(self, s1: str, s2: str) -> bool:
10+
pass

Stack/LC42TrappingRainWater.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
class Solution:
4+
def trap(self, height: List[int]) -> int:
5+
stk = []
6+
ret = 0
7+
8+
for i in range(len(height)):
9+
while stk and height[stk[-1]] <= height[i]:
10+
top = stk[-1]
11+
stk.pop()
12+
13+
if not stk:
14+
break
15+
16+
ret += (i - stk[-1] - 1) * (min(height[i], height[stk[-1]]) - height[top])
17+
18+
stk.append(i)
19+
20+
return ret

Stack/LC716MaxStack.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from collections import OrderedDict
2+
3+
class Node:
4+
def __init__(self, val=None, next=None, prev=None):
5+
self.val = val
6+
self.next = next
7+
self.prev = prev
8+
9+
class MaxStack:
10+
11+
def __init__(self):
12+
self.orderedDict = OrderedDict() # int_val -> list of Nodes with same int val
13+
self.tailNode = Node()
14+
self.headNode = Node()
15+
self.tailNode.next = self.headNode
16+
self.headNode.prev = self.tailNode
17+
18+
def push(self, x: int) -> None:
19+
curNode = Node(x)
20+
self.headNode.prev.next = curNode
21+
curNode.next = self.headNode
22+
curNode.prev = self.headNode.prev
23+
self.headNode.prev = curNode
24+
25+
if x in self.orderedDict:
26+
self.orderedDict[x].append(curNode)
27+
else:
28+
self.orderedDict[x] = [curNode]
29+
30+
def pop(self) -> int:
31+
pass
32+
33+
def top(self) -> int:
34+
pass
35+
36+
def peekMax(self) -> int:
37+
pass
38+
39+
def popMax(self) -> int:
40+
pass
41+
42+
43+
# Your MaxStack object will be instantiated and called as such:
44+
# obj = MaxStack()
45+
# obj.push(x)
46+
# param_2 = obj.pop()
47+
# param_3 = obj.top()
48+
# param_4 = obj.peekMax()
49+
# param_5 = obj.popMax()
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
class Solution:
3+
def largestRectangleArea(self, heights: List[int]) -> int:
4+
ret = 0
5+
stk = []
6+
heights.append(-1)
7+
8+
for i in range(len(heights)):
9+
while stk and heights[stk[-1]] > heights[i]:
10+
top = stk.pop()
11+
if not stk:
12+
ret = max(ret, heights[top] * i)
13+
else:
14+
ret = max(ret, heights[top] * (i - stk[-1] - 1))
15+
stk.append(i)
16+
17+
return ret
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import collections
2+
from typing import List
3+
4+
class Solution:
5+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
6+
if not nums: return []
7+
self.counter = collections.Counter(nums)
8+
keys = list(self.counter.keys())
9+
idx = self.partition(keys, k, 0, len(keys)-1)
10+
return keys[:idx+1]
11+
12+
def partition(self, keys, k, start, end) -> int:
13+
if start >= end: # corner case: keys is empty. start = 0, end = -1
14+
return start
15+
left, right = start - 1, end + 1
16+
17+
pivot = self.counter[keys[(left + right) // 2]]
18+
while left < right:
19+
while True:
20+
left += 1
21+
if self.counter[keys[left]] <= pivot:
22+
break
23+
while True:
24+
right -= 1
25+
if self.counter[keys[right]] >= pivot:
26+
break
27+
if left < right:
28+
keys[left], keys[right] = keys[right], keys[left]
29+
if k <= right - start + 1:
30+
return self.partition(keys, k, start, right)
31+
else:
32+
return self.partition(keys, k - (right - start + 1), right + 1, end)

TwoPointers/LC42TrappingRainWater.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
'''
22
复杂度
33
时间 O(n) 空间(1)
44
思路:双指针->相遇问题

0 commit comments

Comments
 (0)