This commit is contained in:
parent
fe48fc667a
commit
ec9e48aafa
1 changed files with 22 additions and 0 deletions
22
medium/find_the_punishment_number_of_an_integer.py
Normal file
22
medium/find_the_punishment_number_of_an_integer.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# 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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue