Advertisement
Guest User

Untitled

a guest
Oct 6th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Runtime: 84 ms, faster than 38.21% of JavaScript online submissions for Find Pivot Index.
  3. Memory Usage: 40 MB, less than 50.00% of JavaScript online submissions for Find Pivot Index.
  4.  */
  5. const pivotIndex = nums => {
  6.     let leftMap = getSumMap(nums)
  7.     let rightMap = getSumMap(nums.reverse())
  8.     let result = -1
  9.     Object.keys(leftMap).some((index) => {
  10.         if (leftMap[index] === rightMap[nums.length - 1 - index]) {
  11.             result = index
  12.             return true
  13.         }
  14.         return false
  15.     })
  16.     return result
  17. };
  18.  
  19. function getSumMap(nums) {
  20.     let map = {}
  21.     map[0] = 0
  22.     nums.reduce((accumulator, currentValue, index) => {
  23.         let sum = accumulator + currentValue
  24.         map[index + 1] = sum
  25.         return sum
  26.     }, 0)
  27.     return map
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement