You are viewing a single comment's thread. Return to all 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; }
Seems like cookies are disabled on this browser, please enable them to open this website
Travel around the world
You are viewing a single comment's thread. Return to all comments →
simply O(n) algo