You are viewing a single comment's thread. Return to all comments →
O(n), there's a nested while loop but it's actually O(n)
int pylons(int k, vector<int> arr) { int currentLocation = 0; int powerPlants = 0; while (currentLocation < arr.size()) { int temp; if (currentLocation + k - 1 < arr.size()) temp = currentLocation + k - 1; else temp = arr.size() - 1; while (arr[temp] == 0 and temp >= max(0, currentLocation - k + 1)) temp--; if (temp == max(0, currentLocation - k + 1) - 1) return -1; powerPlants++; currentLocation = temp + k; } return powerPlants; }
Seems like cookies are disabled on this browser, please enable them to open this website
Goodland Electricity
You are viewing a single comment's thread. Return to all comments →
O(n), there's a nested while loop but it's actually O(n)