Skip to content

Commit a458514

Browse files
committed
Sync LeetCode submission Runtime - 1116 ms (48.86%), Memory - 17.7 MB (92.86%)
1 parent 47f74c7 commit a458514

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<p>Given a positive integer <code>n</code>, return <em>the <strong>punishment number</strong></em> of <code>n</code>.</p>
2+
3+
<p>The <strong>punishment number</strong> of <code>n</code> is defined as the sum of the squares of all integers <code>i</code> such that:</p>
4+
5+
<ul>
6+
<li><code>1 &lt;= i &lt;= n</code></li>
7+
<li>The decimal representation of <code>i * i</code> can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals <code>i</code>.</li>
8+
</ul>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> n = 10
15+
<strong>Output:</strong> 182
16+
<strong>Explanation:</strong> There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
17+
- 1 since 1 * 1 = 1
18+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
19+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
20+
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> n = 37
27+
<strong>Output:</strong> 1478
28+
<strong>Explanation:</strong> There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
29+
- 1 since 1 * 1 = 1.
30+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
31+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
32+
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
33+
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= n &lt;= 1000</code></li>
41+
</ul>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Approach 2: Recursion of Strings
2+
3+
# Time: O(n * 2 ^ (log n))
4+
# Space: O(log n)
5+
6+
class Solution:
7+
def can_partition(self, string_num, target):
8+
# Valid partition
9+
if not string_num and target == 0:
10+
return True
11+
12+
# Invalid partition
13+
if target < 0:
14+
return False
15+
16+
# Recursively check all partitions for a valid partition
17+
for index in range(len(string_num)):
18+
left = string_num[: index + 1]
19+
right = string_num[index + 1 :]
20+
left_num = int(left)
21+
22+
if self.can_partition(right, target - left_num):
23+
return True
24+
25+
return False
26+
27+
def punishmentNumber(self, n: int) -> int:
28+
punishment_sum = 0
29+
30+
for current_num in range(1, n + 1):
31+
squared_num = current_num * current_num
32+
33+
if self.can_partition(str(squared_num), current_num):
34+
punishment_sum += squared_num
35+
36+
return punishment_sum
37+

0 commit comments

Comments
 (0)