Created
November 4, 2020 17:51
-
-
Save linxinemily/41913eca42e35d6bc6c64371a2b42e86 to your computer and use it in GitHub Desktop.
solution for interview question
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 solution(votes) { | |
function findPairs(votes, arr = [0,0], curr = 0 ) { | |
let next_idx = curr+1 | |
if (!votes[next_idx]) { | |
return [ | |
[arr[0] + votes[curr], arr[1]], | |
[arr[0] , arr[1] + votes[curr]] | |
] | |
} | |
return findPairs(votes, [arr[0] + votes[curr], arr[1]], next_idx).concat(findPairs(votes, [arr[0], votes[curr] + arr[1]], next_idx)) | |
} | |
let result = [] | |
findPairs(votes).forEach(arr => { | |
if (!result.find(arr2 => arr2[0] === arr[0])) { | |
result.push(arr) | |
} | |
}); | |
return result | |
} | |
solution([2,8,10]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment