-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters