hash滑窗|dp

lcr107

多源bfs

注意init ret -1防重复入队

++if(x>=0 && x<m && y>=0 && y<n && retxy == -1)
// 最近距离 = 相邻0的距离 + 1
retxy = retab + 1;
++

class Solution {

typedef pair<int,int> pii;

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

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

public:

vector<vector<int>> updateMatrix(vector<vector<int>>& mat)

{

queue<pii> q;

int m=mat.size(),n=mat0.size();

vector<vector<int>> ret(m,vector<int>(n,0));

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

{

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

{

if(matij==0)

{

q.push({i,j});

}

else

{

// 标记未处理的1为-1

retij = -1;

}

}

}

while(!q.empty())

{

auto a,b = q.front();

q.pop();

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

{

int x=a+dxk,y=b+dyk;

++if(x>=0 && x<m && y>=0 && y<n && retxy == -1)
{
// 最近距离 = 相邻0的距离 + 1
retxy = retab + 1;
++

q.push({x,y});

}

}

}

return ret;

}

};

lcr17

滑动窗口+两个哈希表

在s里找包含t所有字符的最短子串,找不到就返回空

class Solution {

public:

string minWindow(string s, string t) {

unordered_map<char, int> tag;

unordered_map<char, int> hash;

for (char c : t) {

tagc++;

}

int cnt = 0;

int tcnt = t.size();

int l = 0, r = 0;

int minLen = INT_MAX;

int start = 0;

int n = s.size();

while (r < n) {

char c = sr;

hashc++;

if (hashc <= tagc) {

cnt++;

}

r++;

while (cnt == tcnt)

{

++if (r - l < minLen) {++

++minLen = r - l;++

++start = l;++

}

char leftChar = sl;

++hashleftChar--;++

++if (hashleftChar < tagleftChar)++

++cnt--;++

l++;

}

}

return minLen == INT_MAX ? "" : s.substr(start, minLen);

}

};

lc1186

class Solution {

public:

int maximumSum(vector<int>& arr) {

int n=arr.size();

bool flag=false;

int maxNum=INT_MIN;

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

if(arri>0){

flag=true;

break;

}

maxNum=max(maxNum,arri);

}

if(flag==false)return maxNum;

vector<int>pre(n+2),nex(n+2);

int add=0;

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

add+=arri-1;

++if(add<arri-1) add=arri-1;++

++prei=add>0?add:0;++

}

add=0;

for(int i=n;i>=1;i--){

add+=arri-1;

if(add<arri-1)add=arri-1;

nexi=add>0?add:0;

}

int ans=INT_MIN;

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

++ans=max(max(ans,prei-1+nexi+1+arri-1),prei-1+nexi+1);++

}

return ans;

}

};

相关推荐
复杂网络15 分钟前
AI 不睡觉,但它比你更会做实验
算法
贵慜_Derek33 分钟前
MAI-04|干净数据在工程上意味着什么:MAI 预训练数据治理
人工智能·算法·llm
vibecoding日记18 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr213821 小时前
Verilog参数化游程编码RLE模块
算法
望易21 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络1 天前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron2 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩2 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek