hash滑窗|dp

lcr107

多源bfs

注意init ret -1防重复入队

++if(x>=0 && x<m && y>=0 && y<n && ret[x][y] == -1)
// 最近距离 = 相邻0的距离 + 1
ret[x][y] = ret[a][b] + 1;
++

class Solution {

typedef pair<int,int> pii;

int dx[4]={0,0,1,-1};

int dy[4]={1,-1,0,0};

public:

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

{

queue<pii> q;

int m=mat.size(),n=mat[0].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(mat[i][j]==0)

{

q.push({i,j});

}

else

{

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

ret[i][j] = -1;

}

}

}

while(!q.empty())

{

auto [a,b] = q.front();

q.pop();

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

{

int x=a+dx[k],y=b+dy[k];

++if(x>=0 && x<m && y>=0 && y<n && ret[x][y] == -1)
{
// 最近距离 = 相邻0的距离 + 1
ret[x][y] = ret[a][b] + 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) {

tag[c]++;

}

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 = s[r];

hash[c]++;

if (hash[c] <= tag[c]) {

cnt++;

}

r++;

while (cnt == tcnt)

{

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

++minLen = r - l;++

++start = l;++

}

char leftChar = s[l];

++hash[leftChar]--;++

++if (hash[leftChar] < tag[leftChar])++

++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(arr[i]>0){

flag=true;

break;

}

maxNum=max(maxNum,arr[i]);

}

if(flag==false)return maxNum;

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

int add=0;

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

add+=arr[i-1];

++if(add<arr[i-1]) add=arr[i-1];++

++pre[i]=add>0?add:0;++

}

add=0;

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

add+=arr[i-1];

if(add<arr[i-1])add=arr[i-1];

nex[i]=add>0?add:0;

}

int ans=INT_MIN;

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

++ans=max(max(ans,pre[i-1]+nex[i+1]+arr[i-1]),pre[i-1]+nex[i+1]);++

}

return ans;

}

};

相关推荐
Shinom1ya_6 小时前
算法 day 42
数据结构·算法·leetcode
earthzhang20217 小时前
【2051】【例3.1】偶数
开发语言·数据结构·算法·青少年编程·图论
专注VB编程开发20年7 小时前
.net c#音频放大,音量增益算法防止溢出
算法·c#·音频处理·录音·音量增益·增益控制
唯道行7 小时前
计算机图形学·6 OpenGL编程3 谢尔宾斯基垫与三维编程
人工智能·算法·计算机视觉·计算机图形学·三维·谢尔宾斯基垫
高山上有一只小老虎7 小时前
求最大连续bit数
java·算法
Dylan的码园7 小时前
以二叉树问题为基础的递归调试学习(上)
java·学习·算法·leetcode·r-tree
少许极端7 小时前
算法奇妙屋(九)-栈
java·数据结构·算法·
CoovallyAIHub8 小时前
未来已来:从 CVPR & ICCV 观察 2025→2026 年计算机视觉的七大走向
深度学习·算法·计算机视觉
apcipot_rain8 小时前
CSP集训错题集 第八周 主题:基础图论
算法·图论