diff --git "a/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" "b/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" new file mode 100644 index 0000000..29235f9 --- /dev/null +++ "b/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" @@ -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) \ No newline at end of file diff --git "a/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" "b/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" new file mode 100644 index 0000000..98e67dd --- /dev/null +++ "b/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" @@ -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) \ No newline at end of file diff --git "a/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.py" "b/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.py" new file mode 100644 index 0000000..d49e963 --- /dev/null +++ "b/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.py" @@ -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) \ No newline at end of file diff --git "a/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" "b/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" new file mode 100644 index 0000000..8fc30fe --- /dev/null +++ "b/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" @@ -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 \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" "b/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" index 35e1ef0..50bd4bf 100644 --- "a/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" +++ "b/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" @@ -5,4 +5,14 @@ def getLeastNumbers(self, arr, k): :type k: int :rtype: List[int] """ - return sorted(arr)[:k] \ No newline at end of file + 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] \ No newline at end of file