diff --git a/javascript/1780-check-if-number-is-a-sum-of-powers-of-three.js b/javascript/1780-check-if-number-is-a-sum-of-powers-of-three.js new file mode 100644 index 000000000..4edae46fc --- /dev/null +++ b/javascript/1780-check-if-number-is-a-sum-of-powers-of-three.js @@ -0,0 +1,18 @@ +/** + * Brute Force | Recursion | Math + * Time O(2^14) | Space O(1) + * https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three + * @param {number} n + * @return {boolean} + */ +var checkPowersOfThree = function(n) { + + const dfs = (currSum, power) => { + + if (currSum === n) return true; + if (power > 14) return false; + return dfs(currSum + 3**power, power+1) || dfs(currSum, power+1); + } + + return dfs(0, 0); +};