5356. Lucky numbers in matrices
class Solution: def luckyNumbers (self, matrix: List[List[int]]) -> List[int]: rmin = [min(x) for x in matrix] cmax =  n = len(matrix) m = len(matrix) for i in range(m): maxv = matrix[i] for j in range(1, n): maxv = max(maxv, matrix[j][i]) cmax.append(maxv) ans =  for i in range(n): for j in range(m): if matrix[i][j] == rmin[i] and matrix[i][j] == cmax[j]: ans.append(matrix[i][j]) return ans
5357. Design a stack that supports incremental operations
Create self. S =  as stack. However, the first element in S is the real value, and the second is the added value. Each increment operation directly adds the elements in the stack, but records them in the list of elements at the corresponding position in the stack. They are not added up until pop, and are accumulated to the previous element.
class CustomStack: def __init__(self, maxSize: int): self.size = 0 self.maxSize = maxSize self.s =  def push(self, x: int) -> None: if self.size < self.maxSize: self.s.append((x, 0) self.size += 1 def pop(self) -> int: if self.size == 0: return -1 x, inc = self.s.pop() if len(self.s) > 0: self.s[-1] += inc self.size -= 1 return x + inc def increment(self, k: int, val: int) -> None: if self.size > 0: k -= 1 k = min(k, self.size-1) self.s[k] += val
5179. Balance the binary search tree
My method is to traverse the tree – > turn to array – > sort – > build a binary search tree
class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: ls =  def vis(t): if not t: return ls.append(t) vis(t.left) vis(t.right) vis(root) ls.sort(key=lambda t:t.val) def build(l, r): if l == r: ls[l].left = None ls[l].right = None return ls[l] if l + 1 == r: ls[r].left = ls[l] ls[l].left = None ls[l].right = None ls[r].right = None return ls[r] mid = (l+r)>>1 ls[mid].left = build(l, mid-1) ls[mid].right = build(mid+1, r) return ls[mid] return build(0, len(ls)-1)
5359. Maximum team performance value
First, arrange the efficiency from large to small, and set the rigid opening efficiency as the maximum value. This is that only the person with the highest efficiency can be selected, and then continuously reduce the efficiency. The lower the efficiency, the more people can be used. Each time, select k (if there are not enough people, select as many people as possible) who meet the efficiency requirements and have the highest speed.
Use the priority queue to maintain the speed of the currently selected everyone, so that the slowest person can be eliminated each time
import queue class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: es =  for i in range(n): es.append((efficiency[i], speed[i])) es.sort(reverse=True) q = queue.PriorityQueue() q.put(es) mins = es sm = mins mine = es ans = sm * mine maxv = ans cur_min = -1 for i in range(1, n): if i < k: q.put(es[i]) sm += es[i] mine = min(mine, es[i]) maxv = max(maxv, sm*mine) else: if cur_min == -1: cur_min = q.get() if cur_min < es[i]: sm += es[i] sm -= cur_min cur_min = -1 mine = min(mine, es[i]) maxv = max(maxv, sm*mine) q.put(es[i]) return maxv % 1000000007