摘要:LeetCode152 乘积最大子数组 同时统计min和max即可考虑正负关系 class Solution(object): def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ f_min, f_max, l
阅读全文
摘要:LeetCode105 从前序与中序遍历序列构造二叉树 模板题目. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.va
阅读全文
摘要:LeetCode229 多数元素 II 通过消除元素的方法确定候选, 即候选元素可以被消除$\frac{n}{3}$次. class Solution: def majorityElement(self, nums: List[int]) -> List[int]: proposal_1, prop
阅读全文
摘要:LeetCode330 按要求补齐数组 贪心维护当前数字可覆盖区域, $r$表示$[1, r - 1]$区间被覆盖 对于当前$num[i]$, 如果$num[i] \le r$, 维护$r = r + num[i]$ 否则当前最优填入数字为$r$, 可以将区间扩展为$[1, 2\times r -
阅读全文
摘要:LeetCode495 提莫攻击 贪心维护最右 class Solution: def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: time, n, r = 0, len(timeSeries),
阅读全文
摘要:LeetCode215 数组中的第K个最大元素 快排写法 class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: k = len(nums) - k def qsort(l, r): if l == r: r
阅读全文
摘要:LeetCode875 爱吃香蕉的珂珂 二分答案 class Solution: def minEatingSpeed(self, piles: List[int], h: int) -> int: l, r = 1, max(piles) def check(k): time = 0 for c
阅读全文
摘要:LeetCode面试题 16.16. 部分排序 记录前缀max和后缀min,采用双指针拟合答案 class Solution: def subSort(self, array: List[int]) -> List[int]: n = len(array) if n == 0: return [-1
阅读全文
摘要:LeetCode剑指 Offer 60. n个骰子的点数 dp记录状态数量,数学解法会是更快的解法 class Solution: def dicesProbability(self, n: int) -> List[float]: m, p, l, tot = 6, [1, 2, 3, 4, 5,
阅读全文
摘要:LeetCode面试题 08.11. 硬币 完全背包模板题 class Solution: def waysToChange(self, n: int) -> int: m, p, mod = 4, [1, 5, 10, 25], 1000000007 dp = [0 for i in range(
阅读全文
摘要:LeetCode135 分发糖果 贪心 分别考虑左右,对于左侧,如果 $ratings[i] > ratings[i - 1]$,则 $left[i] = left[i - 1] + 1$,否则 $left[i] = 1$ 右侧考虑与左侧相同 最终对于每一个位置取 $max$ class Solut
阅读全文
摘要:LeetCode315 计算右侧小于当前元素的个数 离散化+树状数组 class Solution: def countSmaller(self, nums: List[int]) -> List[int]: clean = list(set(nums)) clean.sort() n = len(
阅读全文
摘要:LeetCode208 实现 Trie (前缀树) 前缀树模板 class Trie: def __init__(self): self.children = [None] * 26 self.end = False def insert(self, word: str) -> None: node
阅读全文
摘要:LeetCode810 黑板异或游戏 只有当数组个数为偶数,或者所有数异或和为0时先手必胜 class Solution: def xorGame(self, nums: List[int]) -> bool: n, xor = len(nums), 0 if n % 2 == 0: return
阅读全文
摘要:LeetCode289 生命游戏 使用扩展标签保留更新之前的信息 这里原来是0,现在是1,标记为2;原来是1,现在是0,标记为-1。保留本轮更新之前的值 最后在遍历更新为0或1 class Solution: def gameOfLife(self, board: List[List[int]])
阅读全文
摘要:LeetCode394 字符串解码 栈模拟解码过程,栈顶记录当前子串(当前[]内字串)的重复次数和上一层(更外一层的[]内)该子串的前缀 class Solution: def decodeString(self, s: str) -> str: stack, ans, multi = [], ''
阅读全文
摘要:LeetCode31 下一个排列 由后向前扫数组,寻找第一个 $nums[i] < nums[i+1]$,表示 $i$ 之后的子数组为降序,即字典序最大 所以下一个排列则是将后面子数组中大于 $nums[i]$ 的所有数字中的最小值与 $nums[i]$ 交换,然后正序排列后续数组,即字典序最小 c
阅读全文
摘要:LeetCode926 将字符串翻转到单调递增 处理前缀1的个数,遍历每种符合单调递增的情况,记录修改最小值 class Solution: def minFlipsMonoIncr(self, s: str) -> int: n, pre, ans = len(s), [int(s[0])], l
阅读全文
摘要:LeetCode673 最长递增子序列的个数 贪心 + 前缀和 + 二分查找 $q[i][]$ 数组表示所有能成为长度为 $i$ 的最长上升子序列的末尾元素的值 $cnt[i][j]$ 记录以 $q[i][j]$ 为结尾的最长上升子序列的个数 参考 class Solution: def findN
阅读全文
摘要:LeetCode560 和为 K 的子数组 前缀和 + 哈希 class Solution: def subarraySum(self, nums: List[int], k: int) -> int: pre_count, pre, ans = {}, 0, 0 pre_count[0] = 1
阅读全文