0% found this document useful (0 votes)
11 views2 pages

DSA Notes-22 July

Uploaded by

shabnumalik111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

DSA Notes-22 July

Uploaded by

shabnumalik111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Rotten Oranges:

class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int cntFresh=0;
queue<pair<pair<int,int>,int>>q;
int rows=grid.size();
int cols=grid[0].size();

for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(grid[i][j]==2){
q.push({{i,j},0});
}
else if(grid[i][j]==1){
cntFresh++;
}
}
}

int maxTime=0;
while(!q.empty()){
int x=q.front().first.first;
int y=q.front().first.second;
int time=q.front().second;

q.pop();

maxTime=max(maxTime,time);
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

for(int t=0;t<4;t++){
int newX=x+dx[t];
int newY=y+dy[t];

if(newX>=0&&newX<rows&&newY>=0&&newY<cols&&grid[newX][newY]==1){
cntFresh--;
grid[newX][newY]=2;
q.push({{newX,newY},time+1});
}
}
}
if(cntFresh==0){
return maxTime;
}
else{
return -1;
}
}
};

Minimum Multiplications to reach End:

class Solution {
public:

int mod=(int)(1e5);
int minimumMultiplications(vector<int>& arr, int start, int end) {
vector<int>visi(1e5,0);
queue<pair<int,int>>q;
q.push({start,0});
visi[start]=1;
while(!q.empty()){
int value=q.front().first;
int steps=q.front().second;
q.pop();

if(value==end){
return steps;
}

for(int i=0;i<arr.size();i++){
int multipliedValue=(value*arr[i])%mod;
if(visi[multipliedValue]==0){
q.push({multipliedValue,steps+1});
visi[multipliedValue]=1;
}
}
}
return -1;
}
};

You might also like