Created
July 17, 2018 01:42
-
-
Save jyotiarora2610/d699c55ca9965552b9225956abd65d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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