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;

}

};

相关推荐
其实防守也摸鱼3 小时前
个人学习 Pwn 思路:从基础到漏洞利用的进阶路径
大数据·前端·人工智能·chrome·算法·安全·网络安全
Nuanyt4 小时前
LeetCode Hot 100 链表专题(五)· 2 题(Java)
java·算法·leetcode·链表
w_zero_one7 小时前
递归算法(一)
java·开发语言·算法
风影同志783248 小时前
20 个比特能装下多少行情?——实测拆解 Kronos 的 K 线分词器
算法
写的都是BUG9 小时前
APK 里的钥匙,一直就放在门口:聊聊加固真正该做什么
前端·算法
地平线开发者9 小时前
CLIP 的零样本分类:一组文本如何变成图像分类器?
算法
京东云开发者10 小时前
从“会生成”到“能生产”:萤火虫 AIGC 平台的 AI Infra 实践
算法·aigc·产品
youshi666810 小时前
PID——算法改进
算法
一只不想敲代码的程序猿12 小时前
如何更高效地拆解问题?
算法