摘要:LeetCode剑指 Offer II 093 最长斐波那契数列 class Solution: def lenLongestFibSubseq(self, arr: List[int]) -> int: n, loc, ans = len(arr), {}, 0 for i in range(n)
阅读全文
摘要:LeetCode554 砖墙 哈希 class Solution: def leastBricks(self, wall: List[List[int]]) -> int: wall_len, cnt, m = sum(wall[0]), {}, len(wall) for i in range(m
阅读全文
摘要:LeetCode739 每日温度 class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: ans, stack, n = [0] * len(temperatures), [], len(t
阅读全文
摘要:LeetCode2096 从二叉树一个节点到另一个节点每一步的方向 最近公共祖先的变形题. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None
阅读全文
摘要:LeetCode740 删除并获得点数 LeetCode198 打家劫舍的变形题目 class Solution: def deleteAndEarn(self, nums: List[int]) -> int: maxVal = max(nums) total = [0] * (maxVal +
阅读全文
摘要:LeetCode剑指 Offer II 097 子序列的数目 $f[i][j]$ 表示 $s[:i]$ 包含 $t[:j]$ 子序列的个数 $s[i] == t[i]$ 时, $f[i][j] = f[i - 1][j - 1] + f[i - 1][j]$, 当前 $s[i]$ 用或者不用 $s[
阅读全文
摘要:LeetCode617 合并二叉树 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.l
阅读全文
摘要:LeetCode338 比特位计数 class Solution: def countBits(self, n: int) -> List[int]: def cnt(x: int) -> int: ones = 0 while x > 0: x = x & (x - 1) ones = ones
阅读全文
摘要:[编程题]仓库配送 # 最短路径, Dijskra from cmath import cos from heapq import * from collections import defaultdict N, K, M = list(map(int, input().strip().split(
阅读全文
摘要:[编程题]项目经理 参考, 模板 '''二分图, 匈牙利算法 最大匹配数: 最大匹配的匹配边的数目 最小点覆盖数: 选取最少的点, 使任意一条边至少有一个端点被选择 最大独立数: 选取最多的点, 使任意所选两点均不相连 最小路径覆盖数: 对于一个 DAG (有向无环图), 选取最少条路径, 使得每个
阅读全文
摘要:LeetCode54 螺旋矩阵 class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: m, n = len(matrix), len(matrix[0]) l, r, t, b, cnt, tar,
阅读全文
摘要:LeetCode912 排序数组 class Solution: def sortArray(self, nums: List[int]) -> List[int]: import random def partition(l: int, r: int) -> int: random_p = ran
阅读全文
摘要:LeetCode5 最长回文子串 Manacher 算法 # 最长回文子串, Manacher class Solution: def expend(self, s, left, right): while left >= 0 and right < len(s) and s[left] == s[
阅读全文
摘要:LeetCode48 旋转图像 先沿水平中线交换,再沿主对角线交换 class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-plac
阅读全文
摘要:LeetCode1143 最长公共子序列 最长公共子串 class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m, n = len(text1), len(text2) dp = [[0
阅读全文
摘要:LeetCode130 被围绕的区域 class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ m,
阅读全文
摘要:商汤科技2018校招 [编程题]解码 dp,注意'0'字符的处理 def solver(s, n): if n == 1: return 1 dp = [1] * n if s[1] != '0' and (s[0] == '1' or (s[0] == '2' and s[1] <= '6')):
阅读全文
摘要:LeetCode825 适龄的朋友 二分求解,考虑重复情况 class Solution: def numFriendRequests(self, ages: List[int]) -> int: ans, n, idx = 0, len(ages), {} ages.sort() for i in
阅读全文
摘要:LeetCode354 俄罗斯套娃信封问题 排序后求最长上升子序列 注意对$y$值从大到小排序可以保证相同$x$只取一个$y$ class Solution: def maxEnvelopes(self, envelopes: List[List[int]]) -> int: envelopes.s
阅读全文
摘要:LeetCode689 三个无重叠子数组的最大和 $dp[i][j]$表示前$j$个元素,选$i$个子区间的最优值 对于当前$j$,转移方程考虑是否以第$j$个元素为子区间的最后一个元素 $dp[i][j] = \max(dp[i][j - 1], dp[i - 1][j - k] + pre[j]
阅读全文