摘要:LeetCode233 数字 1 的个数 枚举每一个数位为1时存在多少种满足小于等于n的数字 当前数位大于1,不存在限制,存在数字个数为左边 $\times$ 右边 当前数位等于1,右边存在限制,当左边等于n的部分时,右边必须满足小于等于n的条件 当前数位等于0,右边存在限制,当左边等于n的部分时,
阅读全文
摘要:LeetCode188 买卖股票的最佳时机 IV 对于第 $i$ 只股票,可以进行的操作为作为第 $j$ 只买入或卖出的股票 $buy[i][j]$ 表示对于前 $i$ 只股票,第 $j$ 次买入股票之后的最大收益 $sell[i][j]$ 表示对于前 $i$ 只股票,第 $j$ 次卖出股票之后的最
阅读全文
摘要:LeetCode123 买卖股票的最佳时机 III 最多可以完成两笔交易,即分两段做LeetCode121,处理前缀后缀之后,遍历每一个分段节点所带来的收益取最大 class Solution: def maxProfit(self, prices: List[int]) -> int: pre_g
阅读全文
摘要:LeetCode122 买卖股票的最佳时机 II 贪心计算爬峰收益 class Solution: def maxProfit(self, prices: List[int]) -> int: ans, l = 0, len(prices) for i in range(1, l): ans +=
阅读全文
摘要:LeetCode121 买卖股票的最佳时机 维护当前股票价格最小值 class Solution: def maxProfit(self, prices: List[int]) -> int: ans, cur_min, l = 0, prices[0], len(prices) for i in
阅读全文
摘要:LeetCode124 二叉树中的最大路径和 dfs左右子节点的最大收益 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self
阅读全文
摘要:LeetCode115 不同的子序列 $dp[i][j]$ 表示字符串 $s[:i]$ 中包子序列 $t[:j]$ 的数量 对于当前字符 $s[i]$ 与 $t[j]$: 如果 $s[i] == t[j]$,$dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]$ 如
阅读全文
摘要:LeetCode69 x的平方根 class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ l, r, ans = 0, x, -1 while l <= r: mid = (l + r) // 2 i
阅读全文
摘要:LeetCode72 编辑距离 对单词A删除一个等价于对单词B插入一个字符,基于此我们仅考虑如下三种操作: 对单词A插入一个字符 对单词B插入一个字符 对单词A替换一个字符 class Solution(object): def minDistance(self, word1, word2): ""
阅读全文
摘要:贪心算法 LeetCode3 无重复字符的最长子串 LeetCode31 下一个排列 LeetCode53 最大子数组和 LeetCode55 跳跃游戏 LeetCode121 买卖股票的最佳时机 LeetCode122 买卖股票的最佳时机 II LeetCode123 买卖股票的最佳时机 III
阅读全文
摘要:LeetCode55 跳跃游戏 顺序维护当前位置可到达最远位置,判断其是否可到达最后一个下标。 class Solution: def canJump(self, nums: List[int]) -> bool: l, max_pos = len(nums), 0 for i in range(l
阅读全文
摘要:最大子串和 最大子串和 class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ ans, pre_sum = -1e4 - 1, 0 for i in nums: i
阅读全文
摘要:排序算法 平均时间复杂度 最好时间复杂度 最坏时间复杂度 空间复杂度 排序方式 稳定性 冒泡排序 \(O(n^2)\) \(O(n)\) \(O(n^2)\) \(O(1)\) In-place 稳定 选择排序 \(O(n^2)\) \(O(n^2)\) \(O(n^2)\) \(O(1)\) In
阅读全文
摘要:堆(heap),又被为优先队列(priority queue), 是一种经过排序的完全二叉树,其任一非叶子节点的值均不大于(或不小于)其左孩子和右孩子节点的值。 将 \(O(n)\) 个元素逐一插入到一个空堆中,时间复杂度是 \(O(n\log n)\); heapify() 的过程,时间复杂度是
阅读全文
摘要:[TOC] 输入输出 input() 输入 Python3 中 input() 函数接受一个标准输入数据,返回为 string 类型。以换行(\n)结束。 prompt: 提示信息。 split() 用于输入 split() 做输入处理,通过指定分隔符对字符串进行切片,默认为所有的空字符,包括空格、
阅读全文
摘要:``` / pku3461(Oulipo), hdu1711(Number Sequence) 这个模板 字符串是从0开始的 Next数组是从1开始的 / include include using namespace std; const int N = 1000002; int next[N];
阅读全文
摘要:"洛谷P1198 [JSOI2008]最大数" 简单的线段树单点问题。 问题:读入 和`Q`时,按照读入一个字符会 MLE ,换成读入字符串就可以了。 include using namespace std; define lson l, mid, root 1; if(pos 1; if(R =
阅读全文
摘要:"洛谷P2023 [AHOI2009]维护序列" 区间修改 当我们要修改一个区间时,要保证 $ax+b$ 的形式,即先乘后加的形式。当将区间乘以一个数 $k$ 时,原来的区间和为 $ax+b$ ,乘以 $k$ 得 $k(ax+b)=kax+kb$ 。 区间加一个数更加简单,原来的区间和为 $ax+b
阅读全文
摘要:"HDU4497 GCD and LCM" 如果 $G \% L != 0$ ,那么输出 $0$ 。 否则我们有 $L/G=(p_1^{r_1})\cdot(p_2^{r_2})\cdot(p_3^{r_3})\cdots(p_m^{r_m})$ 。 我们又有: $$ x=(p_1^{i_1})\c
阅读全文
摘要:最小费用最大流模板 SPFA单路增广 include using namespace std; typedef long long ll; define inf 1000000 define mem(a,b) memset(a,b,sizeof(a)) const int N=5000+20; co
阅读全文