随笔分类 -  算法

随手写的
摘要:https://leetcode.cn/problems/sliding-window-maximum/ 简单的滑动窗口,但是与ACM模式的维护数组不同,在leetcode定义单调队列类更加方便 class MyQueue{ // 单调队列实现,递减 Deque<Integer> deque = n 阅读全文
posted @ 2024-04-30 22:27 风乐 阅读(17) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/minimum-path-cost-in-a-grid 思路很简单,实现很恶心,主要由于我习惯于dp数组从1开始而不是0,这样在推导状态时由于grid是从0,0开始,而循环是从dp数组的1,1开始,因此在方程中一旦涉及到grid就需要把i,j 阅读全文
posted @ 2024-04-28 16:16 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/merge-intervals/?envType=study-plan-v2&envId=top-100-liked 合并区间练习题 typedef pair<int,int> PII; vector<PII> segs; class Sol 阅读全文
posted @ 2024-04-25 11:30 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/edit-distance/description/?envType=study-plan-v2&envId=top-100-liked 这是一个难题,关于序列DP的,官方的题解较为难懂,这里有一位前辈解释的很好 这里的状态定义是: dp[i 阅读全文
posted @ 2024-04-23 17:25 风乐 阅读(10) 评论(0) 推荐(0)
摘要:题面如下: https://www.acwing.com/problem/content/1252/ 这题需要从点出发,不需要管是红线还是蓝线,划线了就得合并两点到同一集合,只要当前线还未画之前,这两个端点是同一集合,那么画完后就是一个连通块,线组成的集合构成连通意味着成圈了,即得到答案 #incl 阅读全文
posted @ 2024-04-19 11:12 风乐 阅读(27) 评论(0) 推荐(0)
摘要:题面如下: https://www.acwing.com/problem/content/530/ 大致思路是:合并所有连接的空洞,判断下表面的空洞和上表面的空洞是否是同一集合集合 #include<iostream> #include<cstring> #include<cstdio> #incl 阅读全文
posted @ 2024-04-19 11:05 风乐 阅读(10) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/maximum-score-of-spliced-array/description/ 这一题应该算一个连续最大子数组思维题,要点是根据差数组去做,然后求最值 class Solution { public: int maximumsSpli 阅读全文
posted @ 2024-04-14 20:24 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://www.luogu.com.cn/problem/P1025 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int N = 10 阅读全文
posted @ 2023-07-02 20:50 风乐 阅读(78) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1102/ 数据范围为1e5 实际上还可以再继续细分,加入特判来优化耗时,但是意义不大 #include<iostream> #include<cstring> #include<cstdio> #include<queu 阅读全文
posted @ 2023-06-16 22:40 风乐 阅读(14) 评论(0) 推荐(0)
摘要:朴素Dijkstra https://www.acwing.com/problem/content/description/851/ O(N^2) #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us 阅读全文
posted @ 2023-05-18 17:17 风乐 阅读(21) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/description/804/ #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> usi 阅读全文
posted @ 2023-05-16 17:34 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/find-smallest-letter-greater-than-target/ 简单二分,需要注意的是此题的二分check条件if(letters[mid]>target),这里的大于号是不能为大于等于的 若等于,则最后的l或r下标就会可 阅读全文
posted @ 2023-04-19 20:55 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/search-insert-position/ 简单二分,这里可以判断return,相当于剪枝 这里的写法最后更新后的l或r一定可以使得nums[l] 或者nums[r]>=target 所以退出循环最后的l或r就是第一个大于等于target 阅读全文
posted @ 2023-04-19 20:13 风乐 阅读(25) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/binary-search/ 简单二分 class Solution { public: int search(vector<int>& nums, int target) { int l=0,r=nums.size()-1; while(l 阅读全文
posted @ 2023-04-19 19:59 风乐 阅读(17) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/maximum-subarray/ 1.暴力+前缀和 class Solution { public: int maxSubArray(vector<int>& nums) { const int N = 1e5+10; int sums[N 阅读全文
posted @ 2023-04-16 18:16 风乐 阅读(22) 评论(0) 推荐(0)
摘要:1e5的数据量,要求时间复杂度小于O(n^2) 1.可以排序后判断是否出现相同数字,O(nlogn) class Solution { public: bool containsDuplicate(vector<int>& nums) { sort(nums.begin(),nums.end()); 阅读全文
posted @ 2023-04-12 19:58 风乐 阅读(11) 评论(0) 推荐(0)