Gas Station | Leetcode Top Interview 150

Поділитися
Вставка
  • Опубліковано 18 вер 2024
  • Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies.
    About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer.
    Problem Link - leetcode.com/p...
    Problem Description - There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
    You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
    Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique

КОМЕНТАРІ • 1

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086  Місяць тому +1

    Solution code - class Solution {
    public:
    int canCompleteCircuit(vector& gas, vector& cost) {
    int curr = 0;
    int start = -1;
    int n = gas.size();
    for (int i=0;i= 0) {
    start = i;
    } else {
    curr = 0;
    }
    } else {
    if (curr < 0) {
    curr = 0;
    start = -1;
    }
    }
    }
    if (start == -1) {
    return -1;
    }
    int curr2 = 0;
    for (int i=0;i= 0) {
    return start;
    } else {
    return -1;
    }
    }
    };