if j - i <= 1: return True return s[i] == s[j - 1] and is_palindrome(i + 1, j - 1) @lru_cache(None) def min_cut(j) -> int: assert 0 <= j <= len(s) best = j - 1 for i in range(0, j): if is_palindrome(i, j): best = min(best, min_cut(i) + 1) return best for j in range(len(s)): min_cut(j) return min_cut(len(s)) # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().minCut) t(["aab"], 1) t(["a"], 0) t(["ab"], 1) t(["eegiicgaeadbcfacfhifdbiehbgejcaeggcgbahfcajfhjjdgj"], 42)
return prices[day] else: return 0 if has_stock: # sale case1 = cal(day + 1, transactions - 1) + prices[day] # not sale case2 = cal(day + 1, transactions) return max(case1, case2) else: # buy case1 = cal(day + 1, transactions - 1) - prices[day] # not buy case2 = cal(day + 1, transactions) return max(case1, case2) for day in range(len(prices) - 1, -1, -1): for transaction in [2, 1]: cal(day, transaction) return cal(0, 2) # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().maxProfit) t([[7, 1, 5, 3, 6, 4]], 5)
# # # 1 <= nums.length <= 2 * 10^4 # -2^31 <= nums[i] <= 2^31 - 1 # # # from typing import List # @lc code=start class Solution: def maxSubArray(self, nums: List[int]) -> int: dp = [0] * len(nums) best = dp[-1] = nums[-1] for i in range(len(nums) - 2, -1, -1): dp[i] = max(nums[i], nums[i] + dp[i + 1]) best = max(best, dp[i]) return best # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().maxSubArray) t([[-2, 1, -3, 4, -1, 2, 1, -5, 4]], 6)
if c1 == c2x and not result: result = is_scramble(s1[:i], s2[:i]) and is_scramble( s1[i:], s2[i:]) if c1 == c2y and not result: result = is_scramble(s1[:i], s2[-i:]) and is_scramble( s1[i:], s2[:-i]) if result: return result return result class Solution: def isScramble(self, s1, s2): if Counter(s1) != Counter(s2): return False return is_scramble(s1, s2) # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().isScramble) t(("great", "rgeat"), True) t(("abcde", "caebd"), False) t(("a", "a"), True) t(("a", "b"), False) t(("abcdd", "abdac"), False)
class Solution: def maxCoins(self, nums: List[int]) -> int: nums = [1] + nums + [1] # dp[i][j] 表示 「 射爆 nums[i:j] 中所有气球,而且 nums[i-1] 以及 nums[j] 这两个气球都没有被射爆时 」 所能取得的最高分数 dp = [[-1] * len(nums) for _ in range(len(nums))] for m in range(0, len(nums) - 1): for j in range(m + 1, len(nums)): i = j - m best = 0 for k in range(i, j): best = max( best, dp[i][k] + nums[i - 1] * nums[k] * nums[j] + dp[k + 1][j]) dp[i][j] = best return dp[1][len(nums) - 1] # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().maxCoins) t([[3, 1, 5, 8]], 167) t([[1, 5]], 10)
def run(e): assert len(e) % 2 == 1 if len(e) == 1: return [int(e[0])] if len(e) == 3: return [eval(f"{e[0]}{e[1]}{e[2]}")] result = [] for i in range(len(e) // 2): op = 2 * i + 1 for left in run(e[:op]): for right in run(e[op + 1:]): result.append(eval(f"{left}{e[op]}{right}")) return result class Solution: def diffWaysToCompute(self, input): elements = re.findall(r"([0-9]+|\+|\-|\*)", input) return run(elements) # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().diffWaysToCompute) t(["2-1-1"], [2, 0]) t(["2*3-4*5"], [-34, -10, -14, -10, 10])
res = dp(n, k - 1) + dp(n - 1, k) - dp(n - 1, k - n) mem[n][k] = res return mem[n][k] for i in range(1, n + 1): for j in range(1, k + 1): dp(i, j) return dp(n, k) % MOD # @lc code=end if __name__ == "__main__": from tool import tt t = tt(Solution().kInversePairs) t([1, 0], 1) t([1, 1], 0) t([1, 2], 0) t([1, 3], 0) t([2, 0], 1) t([2, 1], 1) t([2, 2], 0) t([2, 3], 0) t([2, 4], 0) t([3, 0], 1) t([3, 1], 2) t([3, 2], 2) t([3, 3], 1)