Skip to content

Instantly share code, notes, and snippets.

@linxinemily
Created November 4, 2020 17:51
Show Gist options
  • Save linxinemily/41913eca42e35d6bc6c64371a2b42e86 to your computer and use it in GitHub Desktop.
Save linxinemily/41913eca42e35d6bc6c64371a2b42e86 to your computer and use it in GitHub Desktop.
solution for interview question
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