From 99362f49e0cfb455d3a0555aa6821833cab753f2 Mon Sep 17 00:00:00 2001 From: Yudhvir Singh Date: Sun, 16 Jun 2024 18:48:39 -0700 Subject: [PATCH] sum of squared numbers solution 2 --- python/problem_633/solution2.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 python/problem_633/solution2.py diff --git a/python/problem_633/solution2.py b/python/problem_633/solution2.py new file mode 100644 index 0000000..cc1598a --- /dev/null +++ b/python/problem_633/solution2.py @@ -0,0 +1,20 @@ +class Solution: + def judgeSquareSum(self, c: int) -> bool: + sqrt_num = c ** 0.5 + + int_num = int(str(sqrt_num).split(".")[0]) + sqr_num = int_num ** 2 + + if sqr_num == c: + return True + + # memoization + mem = set() + + for i in range(int_num, 0, -1): + sqr_num = i ** 2 + mem.add(sqr_num) + if c - sqr_num in mem: + return True + + return False