Guest User

Untitled

a guest
Jul 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. function candies(n, arr) {
  2. var ans = 0
  3. var values = []
  4. values[0] = 1
  5. for (let i=1; i< n; i++) {
  6. if (arr[i-1] < arr[i]) {
  7. values[i] = values[i-1] +1
  8. } else {
  9. values[i] = 1
  10. }
  11. }
  12.  
  13.  
  14. ans = values[n - 1];
  15. for (let i= n-2 ; i >= 0 ; i--) {
  16. if (arr[i] > arr[i+1]) {
  17. tmp = values[i+1] + 1
  18. } else {
  19. tmp = 1
  20. }
  21.  
  22. ans = ans + Math.max(tmp, values[i])
  23. }
  24. return ans
  25. }
  26.  
  27. var arr = [1, 2, 2]
  28.  
  29. console.log(candies(3, arr))
Add Comment
Please, Sign In to add comment