Skip to content

Instantly share code, notes, and snippets.

@jyotiarora2610
Created July 17, 2018 01:42
Show Gist options
  • Save jyotiarora2610/d699c55ca9965552b9225956abd65d6a to your computer and use it in GitHub Desktop.
Save jyotiarora2610/d699c55ca9965552b9225956abd65d6a to your computer and use it in GitHub Desktop.
function candies(n, arr) {
var ans = 0
var values = []
values[0] = 1
for (let i=1; i< n; i++) {
if (arr[i-1] < arr[i]) {
values[i] = values[i-1] +1
} else {
values[i] = 1
}
}
ans = values[n - 1];
for (let i= n-2 ; i >= 0 ; i--) {
if (arr[i] > arr[i+1]) {
tmp = values[i+1] + 1
} else {
tmp = 1
}
ans = ans + Math.max(tmp, values[i])
}
return ans
}
var arr = [1, 2, 2]
console.log(candies(3, arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment