前言:
算法学习记录不是算法介绍,本文记录的是从零开始的学习过程(见到的例题,代码的理解……),所有内容按学习顺序更新,而且不保证正确,如有错误,请帮助指出。
学习工具:蓝桥OJ,LeetCode
目录
正文:
双指针(快慢指针):
双指针算法是一种常用的算法技巧,常用于数组和字符串中进行快速查找、匹配、排序或移动
并非真的使用指针实现,而是用两个变量表示下标。
蓝桥OJ 1372:美丽区间
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int a[N],S;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,S;cin >> n >> S;
for(int i = 1;i <= n; ++ i)cin >> a[i];
int ans = n+1;
for(int i = 1,j = 0,sum = 0;i <= n; ++ i)
{
//考虑移动j
while(i>j||(j+1<=n&&sum < S))sum+=a[++j];
if(sum >= S)ans = min(ans,j-i+1);
sum -= a[i];
}
cout<< (ans > n ? 0:ans) << '\n';
return 0;
}
蓝桥OJ 1621:挑选子串
重点在于,确定一个 l 后找到 r 的最小值。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
int a[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,m,k;cin >> n>> m >> k;
for(int i = 1;i <= n;i ++)cin >> a[i];
int ans = 0;
for(int i = 1,j = 0,cnt = 0;i <= n;++i){
while(i > j || (j+1 <= n && cnt < k))cnt += (a[++ j] >= m);
if(cnt >= k)ans += n - j + 1;
cnt -=(a[i] >= m);
}
cout << ans << '\n';
return 0;
}
上述快慢指针可以看作是滑动窗口——非固定窗口大小时的情况。
滑动窗口:
滑动窗口算法实在给定特定窗口大小的数组或字符串上执行要求的操作。
一种能够实现优化的算法。
优势:
解决数组/字符串的子元素问题
把嵌套的循环转化为单个的循环,降低时间复杂度。
模型框架(固定窗口大小):
LeetCode 1208:尽可能使字符串相等
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
int n = s.length();
vector<int> diff(n, 0);
for (int i = 0; i < n; i++) {
diff[i] = abs(s[i] - t[i]);
}
int maxLength = 0;
int start = 0, end = 0;
int sum = 0;
while (end < n) {
sum += diff[end];
while (sum > maxCost) {
sum -= diff[start];
start++;
}
maxLength = max(maxLength, end - start + 1);
end++;
}
return maxLength;
}
};
LeetCode 239:滑动窗口最大值
1.优先队列:
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
priority_queue<pair<int, int>> q;
for (int i = 0; i < k; ++i) {
q.emplace(nums[i], i);
}
vector<int> ans = {q.top().first};
for (int i = k; i < n; ++i) {
q.emplace(nums[i], i);
while (q.top().second <= i - k) {
q.pop();
}
ans.push_back(q.top().first);
}
return ans;
}
};
2.单调队列:
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
deque<int> q;
for (int i = 0; i < k; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
}
vector<int> ans = {nums[q.front()]};
for (int i = k; i < n; ++i) {
while (!q.empty() && nums[i] >= nums[q.back()]) {
q.pop_back();
}
q.push_back(i);
while (q.front() <= i - k) {
q.pop_front();
}
ans.push_back(nums[q.front()]);
}
return ans;
}
};
LeetCode 1456:定长字串中元音的最大数目
class Solution {
public:
bool isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
int maxVowels(string s, int k) {
int n = s.size();
int vowel_count = 0;
for (int i = 0; i < k; ++i) {
vowel_count += isVowel(s[i]);
}
int ans = vowel_count;
for (int i = k; i < n; ++i) {
vowel_count += isVowel(s[i]) - isVowel(s[i - k]);
ans = max(ans, vowel_count);
}
return ans;
}
};
蓝桥OJ ? 多重背包单调队列的优化
这题是在动态规划:背包问题中一题的优化方法。

#include <bits/stdc++.h>
#define inf 1'000'000'000'
using LL = long long;
using ld = long double;
using unl = __int128;
using Pair = std::pair<int, int>;
void solve(const int &Case) {
int n, V;
std::cin >> n >> V;
std::vector<int> v(n), w(n), s(n);
for (int i = 0; i < n; i++)std::cin >> w[i] >> v[i] >> s[i];
std::vector<int> f(V + 1, -inf);
f[0] = 0;
for (int i = 0; i < n; i++) {
auto g = f;
std::vector<int>(V + 1, -inf).swap(f);
for (int x = 0; x < w[i]; x++) {
std::deque<Pair> q;
for (int j = x, k = 0; j <= V; j += w[i], k++) {
if (k > 0 && k - q.front().first > s[i])q.pop_front();
int y = g[j] - k * v[i];
while (!q.empty() && q.back().second <= y)q.pop_back();
q.emplace_back(k, y);
f[j] = q.front().second + k * v[i];
}
}
}
std::cout << *std::max_element(f.begin(), f.end()) << '\n';
}
int main() {
// freopen("1.in", "r", stdin);
// freopen("2.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T = 1;
// std::cin >> T;
for (int Case = 1; Case <= T; Case++)solve(Case);
return 0;
}
发出尖锐的爆鸣声~


958

被折叠的 条评论
为什么被折叠?



