Back to course home
0% completed
Vote For New Content
Would a solution involving hashmaps and recursion not also be valid?let happyMap...
Aidan Eglin
Feb 6, 2022
Would a solution involving hashmaps and recursion not also be valid?
let happyMap = {};
function happyNumber(number) {
if (happyMap[number] != undefined) return false;
reduction = number.toString().split("").map(x => parseInt(x)**2).reduce((acc, current, index) => { return acc + current; }, 0);
happyMap[number] = reduction;
if (reduction == 1) return true;
return happyNumber(reduction);
}
1
0
Comments
Comments
H
hj3yoo 4 years ago
Sure, but now you have a solution with O(n) space complexity.
P
pc 4 years ago
isn't creating a linked list also a O(n) space complexity?
M
Michael Jurewicz3 years ago
There is no linked list in this solution. You are only applying a function to your two "pointers"
On this page