We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This seemed so easy after this tip... Why didn't I remember this?! :(
static long candies(int n, int[] v) {
int[] ca = new int[n];
ca[0] = 1;
for (int i = 1; i < n; i++)
if (v[i] > v[i-1])
ca[i] = ca[i-1] + 1;
else
ca[i] = 1;
long res = ca[n-1];
for (int i = n-2; i >= 0; i--) {
if (v[i] > v[i+1])
ca[i] = Math.max(ca[i], ca[i+1] + 1);
res += ca[i];
}
return res;
}
Candies
You are viewing a single comment's thread. Return to all comments →
This seemed so easy after this tip... Why didn't I remember this?! :(