Skip to content

Commit

Permalink
2020-03-19
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Mar 20, 2020
1 parent a018d24 commit 1e69cf0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
15 changes: 15 additions & 0 deletions 1380.矩阵中的幸运数/1380-矩阵中的幸运数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution(object):
def luckyNumbers (self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
row, col = set(), set()

for r in matrix:
row.add(min(r))

for c in zip(*matrix):
col.add(max(c))

return list(row & col)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class CustomStack(object):

def __init__(self, maxSize):
"""
:type maxSize: int
"""
self.stack = []
self.maxSize = maxSize
def push(self, x):
"""
:type x: int
:rtype: None
"""
if len(self.stack) < self.maxSize:
self.stack.append(x)

def pop(self):
"""
:rtype: int
"""
return self.stack.pop() if self.stack else -1


def increment(self, k, val):
"""
:type k: int
:type val: int
:rtype: None
"""
for i in range(min(k, len(self.stack))):
self.stack[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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def balanceBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
def inorder(node):
if not node:
return []
return inorder(node.left) + [node.val] + inorder(node.right)

l = inorder(root)

def generator(l):
if not l:
return None
idx = len(l) // 2
root = TreeNode(l[idx])
root.left = generator(l[:idx])
root.right = generator(l[idx + 1:])
return root

return generator(l)
29 changes: 29 additions & 0 deletions 1383.最大的团队表现值/1383-最大的团队表现值.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution(object):
def maxPerformance(self, n, speed, efficiency, k):
"""
:type n: int
:type speed: List[int]
:type efficiency: List[int]
:type k: int
:rtype: int
"""
from heapq import *
combine = [(speed[i], efficiency[i]) for i in range(n)]
combine = sorted(combine, key = lambda x: - x[1])
res = 0
MOD = 10 ** 9 + 7
min_heap = []
speed_sum = 0
for i in range(n):
s, e = combine[i]
if len(min_heap) < k:
heappush(min_heap, s)
speed_sum += s
else:
if min_heap and min_heap[0] < s:
speed_sum = speed_sum - min_heap[0] + s
heappush(min_heap, s)
heappop(min_heap)

res = max(res, speed_sum * e)
return res % MOD
12 changes: 11 additions & 1 deletion 面试题40.最小的k个数/面试题40-最小的k个数.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ def getLeastNumbers(self, arr, k):
:type k: int
:rtype: List[int]
"""
return sorted(arr)[:k]
from heapq import *

queue = []
for num in arr:
if len(queue) < k:
heappush(queue, -num)
else:
if queue and queue[0] < num:
heappush(queue, -num)
heappop(queue)
return [-item for item in queue]

0 comments on commit 1e69cf0

Please sign in to comment.