Search code examples
c++algorithmvectordata-structuresstack

Reducing the overlapping count number of buses running from a city to different stopage in straight line, and can my current approach be optimised?


Given pair of distance of start distance and end distance, reduces the number of buses if there is an overlapping in two stopage. for e.g:

input n=4
{2,8},{6,10},{12,14},{12,20}
output:2
explanation:route {2,8} and {6,10} overlap so it can be reduced to 1 route as {2,10}
similarly route {12,14} and {12,20} overlap it can be merged to 1.

input n=4
{1,3} ,{7,9}, {4,6},{10,13}
output=4 since there is no overlapping

my approach: I have tried to sort vector<pair<int,int>> in descending order,and push into stack<pair<int,int>> and have used a counter count that counts the unique route and pops the stack checking the condition,till its gets empty. Despite my code appears 100% correct to me but it does not output anything when there is no overlapping in route., for e.g for input n=4, route: {1,3} ,{7,9}, {4,6},{10,13} it does not output anything. looking for help to tackle such problem.

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
bool compare(pair<int,int>p1,pair<int,int>p2){
    return p1.first>p2.first;
}
int main(){
    int n;cin>>n;
    vector<pair<int,int>>v;
    for(int i=0;i<n;i++){
        int u,w;
        cin>>u>>w;
        v.push_back(make_pair(u,w));
    }
    sort(v.begin(),v.end(),compare);
    stack<pair<int,int>>st;
    for(int i=0;i<v.size();i++){
        st.push(v[i]);
    }
    //looking for help from here ,basically i dont understands why my code in stack fails
    int count=0;
    while (!st.empty()){
        pair<int,int>y=st.top();
        st.pop();
        count++;
        if(y.second>=st.top().first){
            if(st.size()>0){
                 st.pop();
            }
        }

    }
    cout<<count;
}

Solution

  • Your error is at if(y.second >= st.top().first). What if st is empty here?

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    using namespace std;
    bool compare(pair<int,int>p1,pair<int,int>p2){
        return p1.first>p2.first;
    }
    int main(){
        int n;cin>>n;
        vector<pair<int,int>>v;
        for(int i=0;i<n;i++){
            int u,w;
            cin>>u>>w;
            v.push_back(make_pair(u,w));
        }
        sort(v.begin(),v.end(),compare);
        stack<pair<int,int>>st;
        for(int i=0;i<int(v.size());i++){
            st.push(v[i]);
        }
        //looking for help from here ,basically i dont understands why my code in stack fails
        int count=0;
        while (!st.empty()){
            pair<int,int>y=st.top();
            st.pop();
            count++;
            if(st.size()>0){
                if(y.second>=st.top().first){
                    st.pop();
                }
            }
        }
        cout<<count<<'\n';
    }
    

    Just move if(st.size() > 0) outside and your code will run properly.