classSolution: defminHeightShelves(self, books: List[List[int]], L: int) -> int: n = len(books) cost = [999999999] * n T = [x[0] for x in books] H = [x[1] for x in books] idx = n - 1 cur_l = 0 cur_max = 0 while cur_l + T[idx] <= L and idx >= 0: cur_l += T[idx] cur_max = max(cur_max, H[idx]) cost[idx] = cur_max idx -= 1 for i inrange(n - 2, -1, -1): cur = T[i] for j inrange(i + 1, n): if cur <= L andmax(H[i: j]) + cost[j] <= cost[i]: cost[i] = max(H[i: j]) + cost[j] cur += T[j] return cost[0]
classSolution: def_split(self, s: str) -> List[str]: iflen(s) == 0: return [] idx = 0 cur = "" cnt_l = 0 ret = [] while idx < len(s): if s[idx] == ","and cnt_l == 0: ret.append(cur) cur = "" else: cur += s[idx] if s[idx] == "(": cnt_l += 1 if s[idx] == ")": cnt_l -= 1 idx += 1 ret.append(cur) return ret defparseBoolExpr(self, s: str) -> bool: if s == "f": returnFalse if s == "t": returnTrue if s[0] == "!": returnnot self.parseBoolExpr(s[2:-1]) if s[0] == "&": s = s[2:-1] l = self._split(s) f = True for x in l: f = f and self.parseBoolExpr(x) ifnot f: returnFalse; returnTrue if s[0] == "|": s = s[2:-1] l = self._split(s) f = False for x in l: f = f or self.parseBoolExpr(x) if f: returnTrue; returnFalse