We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Queues
  4. Truck Tour
  5. Discussions

Truck Tour

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • a_anofriichuk
    6 years ago+ 11 comments

    Queue-based solutuon, just in case...

    struct gasStation {
        int gas;
        int next;
    };
    
    int main() {
        int N;
        cin >> N;
        queue <struct gasStation> route;
        for (int i = 0; i < N; i++) {
            struct gasStation st;
            cin >> st.gas >> st.next;
            route.push(st);
        }
        int start = 0, passed = 0,  gas = 0;
        while (passed < N) {
            struct gasStation st = route.front();
            gas += st.gas;
            route.pop();
    
            if (gas >= st.next) {
                passed += 1;
                gas -= st.next;
            } else {
                start += passed + 1;
                passed = 0; gas = 0;
            }
            route.push(st);
        }
        cout << start << endl;
        
        return 0;
    }
    
    20|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature