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;

}

};

相关推荐
民乐团扒谱机8 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
饮水桶_T8 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
wy_hhxx9 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者9 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK11 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜13 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z14 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
h_a_o777oah14 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
草履虫AI课15 小时前
强化学习从零到一 07|重要性采样:借别人的数据,算自己的期望
算法·强化学习