一:看到有带(只删除一次)的思路:
动态规划
二:二分法
最大的最小
正向看起来能够用数值去累加,去一个个遍历,但是估计慢,而且也不好做。那么逆向。
或者正向数据范围特别大,10^15这样。
三:下一个最大
单调栈
四:总的策略合起来最大(遍历判断要定位的为第i点)
动态规划
五:路径总和
前缀和,代表题目:https://leetcode.cn/problems/path-sum-iii/
六:连续子数组
滑动窗口或者是前缀和+哈希表
代表题目:https://leetcode.cn/problems/contiguous-array/solutions/809683/lian-xu-shu-zu-by-leetcode-solution-mvnm/ 同组用哈希表存
也有可能数据变化成负数好前缀和索引:https://leetcode.cn/problems/A1NYOS/
滑动窗口+二分法
动态规划
七:差分数组
这个一般是针对目标进行的,也是这题https://leetcode.cn/problems/contiguous-array/solutions/809683/lian-xu-shu-zu-by-leetcode-solution-mvnm/,其实蕴含了差分数组的思想,否则你保存1有n个,0有m个,你需要遍历min(n,m)遍,复杂度是o(n^2)的。如果是差分,那么只有1次就行。我只需要获得abs(m-n)即可。可以理解为答案聚类。
还有一题是这个:https://leetcode.cn/problems/minimum-moves-to-make-array-complementary/solutions/502422/jie-zhe-ge-wen-ti-xue-xi-yi-xia-chai-fen-shu-zu-on/
得到的是差分
还有线段树问题可以转换为差分问题:https://leetcode.cn/problems/count-positions-on-street-with-required-brightness/description/
八:排序+双指针
与位置无关,并且具有单调性。题目:https://leetcode.cn/submissions/detail/550154051/
九:0/1字符串问题
1.前缀和解
2.逆向思维转连续
3.双指针(for两层循环的降级) https://leetcode.cn/problems/flip-string-to-monotone-increasing/
十:动态规划划分
字符串类:
如果是单字符串那么 dp[len]或者dp[from][to]
如果是双字符串,那么dp[len1][len2] 遍历标准:以该位置的元素为终点。算是这个范围的区间。或者:https://leetcode.cn/problems/delete-operation-for-two-strings/ 这个遍历标准:以该位置判断是否要带上这个元素。(很像有区间的样子)
数组类:
如果是整体,那么取整体
如果影响+1,或者说有状态,那么状态。
如果说影响+n,可能是连续的:https://leetcode.cn/problems/minimum-number-of-coins-for-fruits/submissions/572360725/ 遍历标准:以该位置的元素为终点。
是否accept该元素:类似于两层for循环(最长上升子序列)和https://leetcode.cn/problems/non-overlapping-intervals/solutions/541543/wu-zhong-die-qu-jian-by-leetcode-solutio-cpsb/。遍历标准:以该位置的元素为终点。
状态类:
状态step类反向动态规划:https://leetcode.cn/problems/knight-probability-in-chessboard/description/ 遍历标准:以该时刻为状态。
数学类:
几何形状堆叠动态规划:https://leetcode.cn/problems/number-of-ways-to-build-house-of-cards/solutions/2848757/2189-jian-zao-zhi-pai-wu-de-fang-fa-shu-4uwx8/
数学找规律:https://leetcode.cn/problems/find-the-derangement-of-an-array/solutions/
题目求总方法数:一开始dp[0][0]=1,后面其他dp状态都是+=
背包类:
0-1背包 遍历标准:以数组长度为终点,但不一定选。和数值作为dp。
完全背包:https://leetcode.cn/problems/coin-change/description/ 遍历标准:以数值作为dp
走台阶类:
走台阶不能重复:https://leetcode.cn/problems/the-number-of-ways-to-make-the-sum/
组合几何类:
长度 + 组合,用3维dp表示有一定if条件的 https://leetcode.cn/problems/number-of-sets-of-k-non-overlapping-line-segments/solutions/450483/da-xiao-wei-k-de-bu-zhong-die-xian-duan-de-shu-mu-/
十一:位运算
https://leetcode.cn/circle/discuss/CaOJ45/
十二:不用额外空间
就地或者指针、位运算
十三:哈希表的key取法
有时候枚举不会降低复杂度,可以用差分、单位置转移。
十四:子序列:
两层for循环 +dp
或者根据长度动态规划:https://leetcode.cn/problems/longest-palindromic-subsequence/solutions/
转移公式为上一个长度。
十五:逆向思维法
这种就属于,问题编程法。
数学:https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/solutions/2945577/liang-chong-fang-fa-dong-tai-gui-hua-shu-hd4i/?envType=daily-question&envId=2024-10-13
同为数学,但是这个是遍历答案:https://leetcode.cn/problems/minimum-operations-to-make-the-integer-zero/solutions/2319632/mei-ju-da-an-pythonjavacgo-by-endlessche-t4co/
或者画一下函数等。
十六:数论
公因数:https://leetcode.cn/problems/chou-shu-lcof/
线性代数:https://leetcode.cn/problems/maximum-linear-stock-score/solutions/2565409/2898-zui-da-xian-xing-gu-piao-de-fen-by-xkboe/
摩尔投票法求n/3的个数 https://leetcode.cn/problems/majority-element-ii/solutions/1058790/qiu-zhong-shu-ii-by-leetcode-solution-y1rn/
减一or除法:
https://leetcode.cn/problems/minimum-number-of-operations-to-make-x-and-y-equal/ 说明问题:/5一定是-1或者+1到临近的,不可能是-1到下一个/5。
举个例子,在某操作中先减少了 7(得到了 5 的倍数),再除以 5,显然不如先减少 2,再除以 5,再减少 1 划算(后者节省了 4 步)。
十七:判断连通性
并查集https://leetcode.cn/problems/redundant-connection/description/?envType=daily-question&envId=2024-10-27
十八:集合遍历
https://leetcode.cn/problems/shopping-offers/?envType=daily-question&envId=2024-11-03(非01集合)
https://leetcode.cn/problems/design-a-food-rating-system/description/?envType=daily-question&envId=2025-02-28 优先队列内查找元素
十九:排列组合
重复数字的排列组合:https://leetcode.cn/problems/combination-sum-ii/
二十:c++版本用set当做平衡二叉树
https://leetcode.cn/problems/my-calendar-i/solutions/1643942/wo-de-ri-cheng-an-pai-biao-i-by-leetcode-nlxr/?envType=daily-question&envId=2025-01-02
还有双队列,模拟比较复杂的用法:
https://leetcode.cn/problems/number-of-orders-in-the-backlog/solutions/2039856/ji-ya-ding-dan-zhong-de-ding-dan-zong-sh-6g22/
二十一:多存多余的,空间换时间
除了自己之外的数据累计-双向遍历前缀:https://leetcode.cn/problems/product-of-array-except-self/solutions/272369/chu-zi-shen-yi-wai-shu-zu-de-cheng-ji-by-leetcode-/
二十二:空间复杂度o(1)
一般要么就是像动态规划一样可迭代,要么就是就地操作
https://leetcode.cn/problems/product-of-array-except-self/solutions/272369/chu-zi-shen-yi-wai-shu-zu-de-cheng-ji-by-leetcode-/
二十三:中位数
中位数用双优先队列:https://leetcode.cn/problems/minimum-operations-to-make-subarray-elements-equal/description/
二十四:区间范围题
掉落方块:https://leetcode.cn/problems/falling-squares/
二十五:字符串滚动哈希
https://leetcode.cn/problems/strings-differ-by-one-character/solutions/
二十六:二叉树复杂的递归
1.递归类似动态规划,得到下一个确定性解当前不确定性
https://leetcode.cn/problems/split-bst/solutions/2782550/776-chai-fen-er-cha-sou-suo-shu-by-storm-9sgw/
二十七:KMP
https://leetcode.cn/problems/remove-all-occurrences-of-a-substring/solutions/847225/shan-chu-yi-ge-zi-fu-chuan-zhong-suo-you-4j08/
学习点:暴力也可以解,可以用.erase或者string_view避免拷贝。并且kmp可以适用在栈操作删除字符串的场景上,由于栈是对后状态无感知的,所以可以用数组去保存跳转位置。
二十八:回文类
快速生成数字回文:https://leetcode.cn/problems/minimum-cost-to-make-array-equalindromic/solutions/2569308/yu-chu-li-hui-wen-shu-zhong-wei-shu-tan-7j0zy/ 思路:不用字符串生成,就是先生成半边的,然后反转一下。
二十九:有点难度的图遍历
岛屿洪水泛滥:https://leetcode.cn/problems/number-of-closed-islands/solutions/?envType=weekly-question&envId=2025-04-13 孤立岛屿,类似并查集思想。
三十:有一点难的滑动窗口
https://leetcode.cn/problems/count-complete-subarrays-in-an-array/solutions/3650491/tong-ji-wan-quan-zi-shu-zu-de-shu-mu-by-ysvhb/?envType=daily-question&envId=2025-04-24 核心思想:求包含了所有元素的子数目,用另外一个变量acc代表前面有多少种可以允许的选择,后续不断遍历。本质原因是单调递增。
https://leetcode.cn/problems/count-of-interesting-subarrays/description/?envType=daily-question&envId=2025-04-25 非单调递增的滑动窗口,用%操作。跟上面的题类似,但是更难,这个时候不要用滑动窗口了,也是涉及窗口左右扩展,用前缀和。
三十一:字符串数字
https://leetcode.cn/problems/monotone-increasing-digits/solutions/521694/dan-diao-di-zeng-de-shu-zi-by-leetcode-s-5908/ 核心思想 寻找最临近的单调递增的数字,除了全部枚举二分,也可用贪心。9为最大。然后依次减。
三十二:约瑟夫环
https://leetcode.cn/problems/elimination-game/solutions/1187589/gong-shui-san-xie-yue-se-fu-huan-yun-yon-x60m/?envType=weekly-question&envId=2025-05-01 奇数偶数删除,一直到最后一个数字
模板题:
迪杰斯特拉模板题:
https://leetcode.cn/problems/minimum-cost-to-buy-apples/description/
并查集模板题:
https://leetcode.cn/submissions/detail/548115116/
c++ map双层嵌套遍历
https://leetcode.cn/problems/minimum-cost-to-buy-apples/description/
拓扑排序模板题:
https://leetcode.cn/problems/employee-importance/description/?envType=daily-question&envId=2024-08-26
优先队列自定义sort:
https://blog.youkuaiyun.com/weixin_52115456/article/details/127606811
数位dp:
https://leetcode.cn/problems/numbers-at-most-n-given-digit-set/solutions/1900101/shu-wei-dp-tong-yong-mo-ban-xiang-xi-zhu-e5dg/?envType=study-plan-v2&envId=bytedance-2023-spring-sprint
中缀转后缀求表达式的值:
ACM算法:
1、线段树:
线段树模板题:https://leetcode.cn/problems/fancy-sequence/
线段树 + 懒标记: https://leetcode.cn/problems/my-calendar-ii/solutions/1678660/wo-de-ri-cheng-an-pai-biao-ii-by-leetcod-wo6n/?envType=daily-question&envId=2025-01-03
区间修改+区间查询:https://leetcode.cn/problems/design-memory-allocator/solutions/2016010/bao-li-mo-ni-by-endlesscheng-bqba/?envType=daily-question&envId=2025-02-25
线段树求区间最大值:https://leetcode.cn/problems/falling-squares/submissions/596004363/
树状数组:https://leetcode.cn/problems/find-the-index-of-permutation/
用值去构建树状数组,0为不存在,1为存在。然后a[i]前缀和代表小于a[i]数的总数目:https://leetcode.cn/problems/count-number-of-teams/solutions/186425/tong-ji-zuo-zhan-dan-wei-shu-by-leetcode-solution/
树上dp:https://leetcode.cn/problems/choose-edges-to-maximize-score-in-a-tree/solutions/1877272/by-goon-4-1cad/
逆序对(右边大于他的个数)归并排序&线段树:https://leetcode.cn/problems/count-of-smaller-numbers-after-self/solutions/1308773/4chong-jie-fa-yi-wang-da-jin-pai-xu-shu-5vvds/?envType=problem-list-v2&envId=segment-tree