Skip to content

Commit 1e69cf0

Browse files
committedMar 20, 2020
2020-03-19
1 parent a018d24 commit 1e69cf0

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def luckyNumbers (self, matrix):
3+
"""
4+
:type matrix: List[List[int]]
5+
:rtype: List[int]
6+
"""
7+
row, col = set(), set()
8+
9+
for r in matrix:
10+
row.add(min(r))
11+
12+
for c in zip(*matrix):
13+
col.add(max(c))
14+
15+
return list(row & col)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class CustomStack(object):
2+
3+
def __init__(self, maxSize):
4+
"""
5+
:type maxSize: int
6+
"""
7+
self.stack = []
8+
self.maxSize = maxSize
9+
def push(self, x):
10+
"""
11+
:type x: int
12+
:rtype: None
13+
"""
14+
if len(self.stack) < self.maxSize:
15+
self.stack.append(x)
16+
17+
def pop(self):
18+
"""
19+
:rtype: int
20+
"""
21+
return self.stack.pop() if self.stack else -1
22+
23+
24+
def increment(self, k, val):
25+
"""
26+
:type k: int
27+
:type val: int
28+
:rtype: None
29+
"""
30+
for i in range(min(k, len(self.stack))):
31+
self.stack[i] += val
32+
33+
34+
# Your CustomStack object will be instantiated and called as such:
35+
# obj = CustomStack(maxSize)
36+
# obj.push(x)
37+
# param_2 = obj.pop()
38+
# obj.increment(k,val)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def balanceBST(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: TreeNode
13+
"""
14+
def inorder(node):
15+
if not node:
16+
return []
17+
return inorder(node.left) + [node.val] + inorder(node.right)
18+
19+
l = inorder(root)
20+
21+
def generator(l):
22+
if not l:
23+
return None
24+
idx = len(l) // 2
25+
root = TreeNode(l[idx])
26+
root.left = generator(l[:idx])
27+
root.right = generator(l[idx + 1:])
28+
return root
29+
30+
return generator(l)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def maxPerformance(self, n, speed, efficiency, k):
3+
"""
4+
:type n: int
5+
:type speed: List[int]
6+
:type efficiency: List[int]
7+
:type k: int
8+
:rtype: int
9+
"""
10+
from heapq import *
11+
combine = [(speed[i], efficiency[i]) for i in range(n)]
12+
combine = sorted(combine, key = lambda x: - x[1])
13+
res = 0
14+
MOD = 10 ** 9 + 7
15+
min_heap = []
16+
speed_sum = 0
17+
for i in range(n):
18+
s, e = combine[i]
19+
if len(min_heap) < k:
20+
heappush(min_heap, s)
21+
speed_sum += s
22+
else:
23+
if min_heap and min_heap[0] < s:
24+
speed_sum = speed_sum - min_heap[0] + s
25+
heappush(min_heap, s)
26+
heappop(min_heap)
27+
28+
res = max(res, speed_sum * e)
29+
return res % MOD

‎面试题40.最小的k个数/面试题40-最小的k个数.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ def getLeastNumbers(self, arr, k):
55
:type k: int
66
:rtype: List[int]
77
"""
8-
return sorted(arr)[:k]
8+
from heapq import *
9+
10+
queue = []
11+
for num in arr:
12+
if len(queue) < k:
13+
heappush(queue, -num)
14+
else:
15+
if queue and queue[0] < num:
16+
heappush(queue, -num)
17+
heappop(queue)
18+
return [-item for item in queue]

0 commit comments

Comments
 (0)