22 lines
745 B
Python
22 lines
745 B
Python
# https://leetcode.com/problems/find-the-punishment-number-of-an-integer
|
|
|
|
class Solution:
|
|
def punishmentNumber(self, n: int) -> int:
|
|
def recurse(v: str, curr_index: int, curr_value: int, target: int):
|
|
if curr_index >= len(v):
|
|
return curr_value == target
|
|
if curr_value > target:
|
|
return False
|
|
for i in range(curr_index, len(v)):
|
|
tmp = curr_value + int(v[curr_index : i + 1])
|
|
if recurse(v, i + 1, tmp, target):
|
|
return True
|
|
return False
|
|
|
|
result = 1
|
|
for i in range(2, n + 1):
|
|
v = i * i
|
|
if recurse(str(v), 0, 0, i):
|
|
result += v
|
|
return result
|
|
|