• + 0 comments

    simply O(n) algo

    int travelAroundTheWorld(long N, const vector<long>& A, const vector<long>& B, long capacity) {
        int answer = 0;
        vector<long> cache(2 * N);
        for (int i = 2 * N - 2; i >= 0; i--) {
            if (capacity < cache[i + 1] + B[i % N]) break;
            if (A[i % N] >= capacity) cache[i] = 0;
            else cache[i] = max(cache[i + 1] + B[i % N] - A[i % N], 0L);
            if (i < N and cache[i] == 0) answer++;
        }
        return answer;
    }