weekly-contest-180

[TOC]

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def addTo(self, d, i, m):
if i in d:
d[i].append(m)
else:
d[i] = [m]

def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
d = {}
for l in matrix:
m = min(l)
for i, v in enumerate(l):
if v==m:
self.addTo(d, i, m)
result = []
for k, v in d.items():
m = max(i[k] for i in matrix)
result.extend(i for i in v if i==m)
return result

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class CustomStack:

def __init__(self, maxSize: int):
self.left = maxSize
self.l = []

def push(self, x: int) -> None:
if self.left>0:
self.l.append(x)
self.left -= 1

def pop(self) -> int:
if self.l:
self.left += 1
result = self.l[-1]
del self.l[-1]
return result
else:
return -1

def increment(self, k: int, val: int) -> None:
for i in range(min(k, len(self.l))):
self.l[i] += val



# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)

3

LeetCode 1382. Balance a Binary Search Tree - AcWing

OHumJwqtc6CyTNa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def getNums(self, root, l):
if not root:
return
self.getNums(root.left, l)
l.append(root.val)
self.getNums(root.right, l)

def build(self, l):
n = len(l)
if n==0:
return None
mid = int(n/2)
root = TreeNode(l[mid])
root.left = self.build(l[:mid])
root.right = self.build(l[mid+1:])
return root


def balanceBST(self, root: TreeNode) -> TreeNode:
l = []
self.getNums(root, l)
if len(l)<3:
return root
return self.build(l)

4

排序+堆(Python) - 最大的团队表现值 - 力扣(LeetCode)

思路:

按efficiency进行降序排列,
遍历排序后的speed和efficiency,在遍历过程中进行以下操作:
(1)将speed放入最小堆中,
(2)保持堆的大小为k,
(3)记录堆中speed之和,
(4)寻找最大团队表现值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from heapq import heappush as push
from heapq import heappop as pop

class Solution:
def __init__(self):
self.mod = 10**9+7

def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
l = sorted(zip(speed, efficiency), key=lambda i: -i[1])
speed_sum = 0
speedl = []
res = 0
for s, e in l:
push(speedl, s)
speed_sum += s
if len(speedl)>k:
speed_sum -= pop(speedl)
res = max(res, speed_sum*e)
return res%self.mod
# 推荐文章
  1.biweekly-contest-22
  2.面试题 01.07 旋转矩阵
  3.chrome 插件
  4.biweekly-contest-23
  5.weekly-contest-180

评论


:D 一言句子获取中...