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;

}

};

相关推荐
026d233 小时前
状态信号只知“其然”不知“其所以然”:变工况机械故障诊断的物理盲区
算法
姜鱼问生3 小时前
备份和服务器在同一台机器上,等于没备份
算法·架构
姜鱼问生4 小时前
别再 chmod 777 了,用 ACL 给它开一扇窗
后端·算法·架构
特立独行的猫a4 小时前
仓颉语言UI开发实战:我用仓颉写了个桌面版力扣
算法·leetcode·ui·tauri·仓颉·cj-tarui
y1su4 小时前
Leetcode 动态规划 模板
java·数据结构·算法·leetcode·动态规划
All for pursuit.5 小时前
【位运算-3】136.只出现一次的数字
数据结构·c++·算法·leetcode
MoRanzhi12035 小时前
106.正则问题(分治)
python·算法·正则表达式·蓝桥杯·递归·分治算法·字符串处理
suhan6 小时前
【数据结构】五、树:7.哈夫曼树、哈夫曼编码
数据结构·算法·排序算法
kkhimself6 小时前
POJ-3126
算法
suhan6 小时前
【数据结构】五、树:6.平衡二叉树AVL
数据结构·算法·排序算法