You are viewing a single comment's thread. Return to all comments →
long long candies(int n, vector<int> arr) { vector<pair<int, int>> v; vector<int> candy(arr.size()); long long total = 0; for (int i=0; i < arr.size(); i++) { v.push_back({arr[i], i}); } sort(v.begin(), v.end()); for (int i=0; i < v.size(); i++) { int p = v[i].second; if ((p == 0 or arr[p] <= arr[p-1]) and (p == arr.size()-1 or arr[p] <= arr[p+1])){ candy[p] = 1; total++; continue; } else if (p != 0 and arr[p] > arr[p-1] and (p == arr.size()-1 or arr[p] <= arr[p+1])) { candy[p] = candy[p-1] + 1; } else if (p != arr.size()-1 and (p ==0 or arr[p] <= arr[p-1]) and arr[p] > arr[p+1]) { candy[p] = candy[p+1] + 1; } else if ((p == 0 or arr[p] > arr[p-1]) and (p == arr.size()-1 or arr[p] > arr[p+1])) { if (p == 0) candy[p] = candy[p+1] + 1; else if (p == arr.size()-1) candy[p] = candy[p-1] + 1; else candy[p] = max(candy[p-1], candy[p+1]) + 1; } total = total + candy[p]; } return total; }
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →