Skip to content

Commit

Permalink
Add updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Oct 19, 2021
1 parent dce7618 commit 3295045
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"iostream": "cpp"
}
}
16 changes: 16 additions & 0 deletions Array/minJumpstorchEnd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// int minJumps(int A[], int n)
// {
// int jumps = 0, curEnd = 0, curFarthest = 0;
// for (int i = 0; i < n; i++)
// {
// if (i > curFarthest)
// return -1;
// curFarthest = max(curFarthest, i + A[i]);
// if (i < n - 1 && i == curEnd)
// {
// jumps++;
// curEnd = curFarthest;
// }
// }
// return jumps;
// }
64 changes: 36 additions & 28 deletions graph/minEdgetomakepath.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
#include<bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
void solve(int src,int dest,vector<int> adj[],int n){
vector<pair<int,int>> newAdj[n];
for(int i=0;i<n;i++){
void solve(int src, int dest, vector<int> adj[], int n)
{
vector<pair<int, int>> newAdj[n];
for (int i = 0; i < n; i++)
{
int u = i;
for(auto v:adj[i]){
newAdj[u].push_back({v,0});
newAdj[v].push_back({u,1});
for (auto v : adj[i])
{
newAdj[u].push_back({v, 0});
newAdj[v].push_back({u, 1});
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,src});
vector<int> cost(n,INT_MAX);
cost[src]=0;
while(!pq.empty()){
auto pnt = pq.top().second;pq.pop();
for(auto x:newAdj[pnt]){
if(x.second+cost[pnt] < cost[x.first]){
cost[x.first] = x.second+cost[pnt];
pq.push({cost[x.first],x.first});
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, src});
vector<int> cost(n, INT_MAX);
cost[src] = 0;
while (!pq.empty())
{
auto pnt = pq.top().second;
pq.pop();
for (auto x : newAdj[pnt])
{
if (x.second + cost[pnt] < cost[x.first])
{
cost[x.first] = x.second + cost[pnt];
pq.push({cost[x.first], x.first});
}
}
}
cout<<"Minimum cost required "<<cost[dest]<<endl;
cout << "Minimum cost required " << cost[dest] << endl;
return;

}
int main(){
int n,e;
int main()
{
int n, e;
// taking 0 based numbering for vertices of graph
cin>>n>>e;
cin >> n >> e;
vector<int> adj[n];
for(int i=0;i<e;i++){
int u,v;
cin>>u>>v;
for (int i = 0; i < e; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
int src,dest;
cin>>src>>dest;
solve(src,dest,adj,n);
int src, dest;
cin >> src >> dest;
solve(src, dest, adj, n);
return 0;
}
Binary file added heap/a.out
Binary file not shown.
53 changes: 53 additions & 0 deletions stack/maxHist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*Easy logic just find nsl and nsr and then compute the area*/
#include <bits/stdc++.h>
using namespace std;
long long getMaxArea(long long arr[], int n)
{
// Your code here
vector<int> nsl(n), nsr(n);
stack<pair<int, int>> st, st1;
for (int i = n - 1; i >= 0; i--)
{
while (!st.empty() && st.top().first >= arr[i])
{
st.pop();
}
if (st.empty())
{
nsr[i] = n;
}
else
{
nsr[i] = st.top().second;
}
st.push({arr[i], i});
}

for (int i = 0; i < n; i++)
{
while (!st1.empty() && st1.top().first >= arr[i])
{
st1.pop();
}
if (st1.empty())
{
nsl[i] = -1;
}
else
{
nsl[i] = st1.top().second;
}
st1.push({arr[i], i});
}
long long int ans = INT_MIN;
for (int i = 0; i < n; i++)
{
ans = max(ans, arr[i] * (nsr[i] - nsl[i] + 1 - 2));
// cout<<arr[i]*(nsr[i]-nsl[i]+1-2)<<" ";
}
// cout<<endl;
return ans;
}
int main()
{
}
65 changes: 65 additions & 0 deletions strings/shortestStringcontPatt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
int n = s1.size(), m = s2.size();
map<char, int> mp1, mp2;
for (auto x : s2)
mp2[x]++;
int rmct = 0, mct = 0;
for (auto x : mp2)
{
rmct += x.second;
}
int left = 0, right = 0;
string ans = "";
while (true)
{
bool f1 = false, f2 = false;
// cout << "Aquiring\n";
while (right < n && mct < rmct)
{
mp1[s1[right]]++;
if (mp1[s1[right]] <= mp2[s1[right]])
{
mct++;
}
string temp = s1.substr(left, right - left + 1);
cout << temp << endl;
right++;
f1 = true;
}

// cout<<"Releasing\n";
while (left < right && mct == rmct)
{
string temp = s1.substr(left, right - 1 - left + 1);
cout << temp << endl;
if (ans.size() == 0 || ans.size() > temp.size())
{
ans = temp;
}

if (mp1[s1[left]] == 1)
{
mp1.erase(s1[left]);
}
else
{
mp1[s1[left]]--;
}
if (mp1[s1[left]] < mp2[s1[left]])
{
mct--;
}
left++;
f2 = true;
}
if (!f1 && !f2)
break;
}
cout << ans << endl;
return 0;
}

0 comments on commit 3295045

Please sign in to comment.