Last active
January 15, 2022 00:20
-
-
Save germanescobar/170329470dc5b85cbcefcd188a2054fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @param {number[]} deliciousness | |
* @return {number} | |
*/ | |
var countPairs = function(deliciousness) { | |
let max = deliciousness[0] | |
for (let i=1; i < deliciousness.length; i++) { | |
if (deliciousness[i] > max) { | |
max = deliciousness[i] | |
} | |
} | |
const memo = {} | |
let i=0 | |
for (let j=0; i < max*2; j++) { | |
i=2**j | |
memo[i] = true | |
} | |
let count = 0 | |
for (let i=0; i < deliciousness.length - 1; i++) { | |
for (let j=i+1; j < deliciousness.length; j++) { | |
const sum = deliciousness[i] + deliciousness[j] | |
if (memo[sum]) { | |
count++ | |
} | |
} | |
} | |
return count | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment