You are viewing a single comment's thread. Return to all comments →
Please tell, why this solution is not working. long candies(int n, vector arr) { long sum = 0; vector c(n, 0); c[0] = 1; for (int i = 1; i < n; i++){ if(arr[i-1] < arr[i]){ c[i] = c[i-1] + 1; } else if(arr[i-1] == arr[i]) c[i] = 1; else{ if(c[i-1] != 1){ c[i] = 1; } else{ c[i] = 1; int start = i; while(c[start] == c[start-1] && start >= 1 && start - 1 >= 0){ c[start-1] += 1; start--; } } } }
sum = std::accumulate(c.begin(), c.end(), sum }
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 →
Please tell, why this solution is not working. long candies(int n, vector arr) { long sum = 0; vector c(n, 0); c[0] = 1; for (int i = 1; i < n; i++){ if(arr[i-1] < arr[i]){ c[i] = c[i-1] + 1; } else if(arr[i-1] == arr[i]) c[i] = 1; else{ if(c[i-1] != 1){ c[i] = 1; } else{ c[i] = 1; int start = i; while(c[start] == c[start-1] && start >= 1 && start - 1 >= 0){ c[start-1] += 1; start--; } }
} }
sum = std::accumulate(c.begin(), c.end(), sum }