bfs|栈

lc1885

满足 di + dj > 0 的 dj 的最小阈值 target (即 -di + 1 )

lower_bound 找到 d 数组中++从 i+1 位置开始第一个大于等于 target 的元素位置++

数组末尾位置减去该位置得到满足条件的 j 的数量,最后累加到结果中

typedef long long ll;

class Solution {

public:

long long countPairs(vector<int>& nums1, vector<int>& nums2)

{

int n = nums1.size();

vector<int> d(n);

for (int i = 0; i < n; ++i) {

di = nums1i - nums2i;

}

sort(d.begin(), d.end());

ll ret = 0;

for (int i = 0; i < n; ++i) {

int target = -di + 1;

int cnt = d.end() - lower_bound(d.begin() + i + 1, d.end(), target);

ret += cnt;

}

return ret;

}

};

lc1018

class Solution {

public:

vector<bool> prefixesDivBy5(vector<int>& nums)

{

int n = nums.size();

vector<bool> ret(n, false);

int num = 0;

for (int i = 0; i < n; i++)

{

num = (num << 1) + numsi;

if (num % 5 == 0)

reti = true;

// 防止num过大溢出,对5取余不影响结果,因为++(a*2 + b) % 5 = ((a%5)*2 + b) % 5
num %= 5;
++

}

return ret;

}

};

lc1950

单调栈遍历数组,计算每个位置作为最小值时的区间长度

记录对应区间的最大最小值

再通过 *++倒序更新++*得到每个长度下的最大最小值

class Solution {

public:

vector<int> findMaximums(vector<int>& nums) {

int n = nums.size();

//求左右延拓长度,并完成初始化更新

stack<int> sta;

sta.push(-1);

vector<int> ans(n, 0);

for(int i = 0; i < n; ++i){

while(sta.top() != -1 && numssta.top() > numsi){

int mi = numssta.top();

sta.pop();

ansi - sta.top() - 2 = max(ansi - sta.top() - 2, mi);

}

sta.push(i);

}

while(sta.top() != -1){

int mi = numssta.top();

sta.pop();

ansn - sta.top() - 2 = max(ansn - sta.top() - 2, mi);

}

// 倒序更新

for(int i = n - 2;i >= 0; i--){

ansi = max(ansi, ansi + 1);

}

return ans;

}

};

画图理清思路 用算法实现出来

lc484

反转连续的D区间

class Solution {

public:

vector<int> findPermutation(string s) {

int n=s.size()+1;

vector<int>ans(n,0);

for(int i=0;i<n;i++)ansi=i+1;

int i=0;

while(i<n){

if(si=='D'){

int j=i+1;

while(j<n&&sj=='D'){

j++;

}

int len=j-i;

++reverse(ans.begin()+i,ans.begin()+i+len+1);++

++//+1多反转一个++

i=j; //i移到下一个非d的地方

}

else i++;

}

return ans;

}

lc340

滑窗

class Solution {

public:

int lengthOfLongestSubstringKDistinct(string s, int k) {

int n=s.size();

unordered_map<char,int> hash;

int cnt=1,l=0,ret=1;

if(k==0) return 0;

hashs\[0]++;

for(int i=1;i<n;i++)

{

if(!hash.count(si))

cnt++;

hashs\[i]++;

while(cnt>k)

{

if(--hashs\[l]==0)

{

cnt--;++hash.erase(sl);++

}

l++;

}

ret=max(ret,i-l+1);

}

return ret;

}

};

lc505

if (p+add<visnxny)

visnxny = p+add;

//update

q.push({nx, ny,p+add})

BFS结合距离记录机制,沿四个方向滚动到底的方式探索迷宫路径

dist数组维护各位置最短距离,仅当新路径更短时更新入队

class Solution

{

int dx4={1,-1,0,0};

int dy4={0,0,1,-1};

typedef tuple<int,int,int> tiii;

public:

int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination)

{

int m = maze.size(), n = maze0.size();

vector<vector<int>> vis(m, vector<int>(n, INT_MAX));

queue<tiii> q;

q.push({start0, start1, 0});

visstart\[0]start\[1] = 0;

int ret=INT_MAX;

while (!q.empty()) {

auto x, y,p = q.front();

q.pop();

if (x == destination0 && y == destination1) {

ret=min(ret,p);

}

for (int k = 0; k < 4; ++k) {

int add=0;

int nx = x, ny = y;

// 沿当前方向滚动到底

while (nx + dxk >= 0 && nx + dxk < m && ny + dyk >= 0 && ny + dyk < n && mazenx + dx\[k]ny + dy\[k] == 0) {

nx += dxk;

ny += dyk;

add++;

}

if (p+add<visnxny) {

visnxny = p+add;

//update

q.push({nx, ny,p+add});

}

}

}

return ret==INT_MAX?-1:ret;

}

};

相关推荐
何以解忧,唯有..31 分钟前
预订酒店问题
算法
No8g攻城狮39 分钟前
【算法】什么是 DFS 与 BFS 及他们的区别
算法·深度优先·宽度优先
zww89491111 小时前
家政派单系统实战指南:从需求分析到智能调度算法落地
算法·需求分析
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 评测实战:用 DeepSeek-R1 当裁判,打造多智能体系统的自动化质量保障体系
人工智能·深度学习·算法·aigc
ZC跨境爬虫1 小时前
LeetCode 88. 合并两个有序数组(双指针详解 + Java Python 实现)
java·python·算法·leetcode
Nil2081 小时前
leetcode 238除了自身以外数组的乘积
数据结构·算法·leetcode
土司大王1 小时前
LeetCode hot100——最长连续序列
数据结构·算法·leetcode
caimouse1 小时前
ReactOS 图形系统分析(17):区域填充 — EngPaint / IntEngPaint(paint.c)
c语言·开发语言·算法
生态学者1 小时前
Ecological Indicators | 机场鸟调的新方法:用 AWM-PC1 读懂干扰梯度下的鸟类群落
人工智能·算法·r语言·微信公众平台
Herbert_hwt2 小时前
第六章 Java深入理解接口、函数式接口与lambda表达式
java·开发语言·算法