摘要:
动态规划 Dynamic Programming • 拆分(Divide): 将一个复杂问题拆分成一系列的简单子问题,每一次解决一个子 问题并将其结果存储起来。理想情况下用基于内存的数据结构。 • 查找(lookup):在下一次遇到相同的子问题的时候,直接查找之前计算过的结果 而不是重新计算。理想情 阅读全文
摘要:
题目描述: 提交: class Solution: def maxDiff(self, num: int) -> int: maxn, minn = num, num for i in range(10): s = str(num).replace(str(i), '9') maxn = max(i 阅读全文
摘要:
题目描述: 提交: class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: a,b = sorted(list(s1)),sorted(list(s2)) n = len(a) if all(a[i] >= b[i] 阅读全文
摘要:
题目描述: 方法一:记忆化递归+状态压缩 * from functools import lru_cache class Solution: def numberWays(self, hats: List[List[int]]) -> int: N = len(hats) M = 41 mod = 阅读全文
摘要:
提交:O(N) class Solution: def destCity(self, paths: List[List[str]]) -> str: dic = {} for i,v in paths: dic[i] = v tmp = paths[0][1] while tmp in dic: t 阅读全文
摘要:
题目描述: 提交:O(N) class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: c = k for i in nums: if i == 1: if c < k: return False else: c 阅读全文
摘要:
题目描述: 方法一:暴力法O(m*max(n,klog(k)) O(k) 使用 vector<int> ans 记录各个行选一个数相加的和11. 记录的方法是,先保存第一行各数12. 然后把第二行的各数拿出来,组合相加13. 对其排列,超过 k 个就不需要保留了14. 相加之后记录回 ans ,下次 阅读全文
摘要:
题目描述: 第一次提交: class Solution: def longestSubarray(self, nums, limit: int) -> int: res = 0 i,j = 0,0 n = len(nums) l = [[nums[0],0],[nums[0],0]] while i 阅读全文
摘要:
题目描述: 方法:中序遍历 O(N) O(N) /* // Definition for a Node. class Node { public int val; public Node left; public Node right; public Node() {} public Node(in 阅读全文
摘要:
方法:二分查找 def binary_search(mountain, target, l, r, key=lambda x: x): target = key(target) while l <= r: mid = (l + r) // 2 cur = key(mountain.get(mid)) 阅读全文