Skip to content

Commit 21056d6

Browse files
committedFeb 15, 2025
Feb-15
1 parent f0b051d commit 21056d6

File tree

3 files changed

+201
-4
lines changed

3 files changed

+201
-4
lines changed
 

‎README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3636

3737
| Status | List | Progress | Notes |
3838
| ----- | ----- | ----- | ----- |
39-
| [15%] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 351/2207 | 5 vips |
39+
| [15%] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 352/2207 | 5 vips |
4040
| [18%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 10/54 | - |
4141
| [47%] | [leetcode-google.md](./list/leetcode-google.md) | 223/471 | 2 vips |
4242
| [48%] | [leetcode-75.md](./list/leetcode-75.md) | 36/75 | - |
@@ -55,7 +55,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
5656
| [🔲] | [9c-top.md](./list/9c-top.md) | 39/56 | 1 vip |
5757

58-
**Solved**: 527 problems
58+
**Solved**: 528 problems
5959

6060
## 类型/Category
6161

@@ -98,7 +98,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
9898

9999
## Math
100100

101-
| Link | Problem(28) | Solution | Tag | Time | Space | Ref |
101+
| Link | Problem(29) | Solution | Tag | Time | Space | Ref |
102102
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
103103
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
104104
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
@@ -112,6 +112,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
112112
| [Leetcode-150](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Evaluate Reverse Polish Notation | [c++](./leetcode/150.evaluate-reverse-polish-notation.cpp), [python3](./leetcode/150.evaluate-reverse-polish-notation.py) | Math | O\(N\) | O\(N\) | - |
113113
| [Leetcode-168](https://leetcode.com/problems/excel-sheet-column-title/) | Excel Sheet Column Title | [c++](./leetcode/168.excel-sheet-column-title.cpp), [python3](./leetcode/168.excel-sheet-column-title.py) | Math | O\(logN\) | O\(1\) | - |
114114
| [Leetcode-172](https://leetcode.com/problems/factorial-trailing-zeroes/) | Factorial Trailing Zeroes | [c++](./leetcode/172.factorial-trailing-zeroes.cpp), [python3](./leetcode/172.factorial-trailing-zeroes.py) | Math | O\(logN\) | O\(1\) | - |
115+
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Math | O\(N \* 2^K\) | O\(K\) | - |
115116
| [Leetcode-202](https://leetcode.com/problems/happy-number/) | Happy Number | [c++](./leetcode/202.happy-number.cpp), [python3](./leetcode/202.happy-number.py) | Math | O\(S\) | O\(1\) | - |
116117
| [Leetcode-12](https://leetcode.com/problems/integer-to-roman/) | Integer To Roman | [c++](./leetcode/12.integer-to-roman.cpp), [python3](./leetcode/12.integer-to-roman.py) | Math | O\(N\) | O\(1\) | - |
117118
| [Leetcode-973](https://leetcode.com/problems/k-closest-points-to-origin/) | K Closest Points To Origin | [c++](./leetcode/973.k-closest-points-to-origin.cpp), [python3](./leetcode/973.k-closest-points-to-origin.py) | Math | O\(NlogN\) | O\(K\) | - |
@@ -889,11 +890,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
889890

890891
## Backtracking
891892

892-
| Link | Problem(14) | Solution | Tag | Time | Space | Ref |
893+
| Link | Problem(15) | Solution | Tag | Time | Space | Ref |
893894
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
894895
| [Leetcode-257](https://leetcode.com/problems/binary-tree-paths/) | Binary Tree Paths | [c++](./leetcode/257.binary-tree-paths.cpp), [python3](./leetcode/257.binary-tree-paths.py) | Backtracking | O\(N\) | O\(H\) | - |
895896
| [Leetcode-40](https://leetcode.com/problems/combination-sum-ii/) | Combination Sum II | [c++](./leetcode/40.combination-sum-ii.cpp), [python3](./leetcode/40.combination-sum-ii.py) | Backtracking | O\(2^N\) | O\(N\) | - |
896897
| [Leetcode-77](https://leetcode.com/problems/combinations/) | Combinations | [c++](./leetcode/77.combinations.cpp), [python3](./leetcode/77.combinations.py) | Backtracking | O\(k \* C\(n, k\)\) | O\(K\) | - |
898+
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Backtracking | O\(N \* 2^K\) | O\(K\) | - |
897899
| [Leetcode-22](https://leetcode.com/problems/generate-parentheses/) | Generate Parentheses | [c++](./leetcode/22.generate-parentheses.cpp), [python3](./leetcode/22.generate-parentheses.py) | Backtracking | O\(2^N\) | O\(N\) | - |
898900
| [Leetcode-51](https://leetcode.com/problems/n-queens/) | N Queens | [c++](./leetcode/51.n-queens.cpp), [python3](./leetcode/51.n-queens.py) | Backtracking | O\(N\!\) | O\(N\) | - |
899901
| [Leetcode-52](https://leetcode.com/problems/n-queens-ii/) | N Queens II | [c++](./leetcode/52.n-queens-ii.cpp), [python3](./leetcode/52.n-queens-ii.py) | Backtracking | O\(N\!\) | O\(N\) | - |
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Tag: Math, Backtracking
2+
// Time: O(N * 2^K)
3+
// Space: O(K)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/bLgWWGhaweM
7+
8+
// Given a positive integer n, return the punishment number of n.
9+
// The punishment number of n is defined as the sum of the squares of all integers i such that:
10+
//
11+
// 1 <= i <= n
12+
// The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.
13+
//
14+
//  
15+
// Example 1:
16+
//
17+
// Input: n = 10
18+
// Output: 182
19+
// Explanation: There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
20+
// - 1 since 1 * 1 = 1
21+
// - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
22+
// - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
23+
// Hence, the punishment number of 10 is 1 + 81 + 100 = 182
24+
//
25+
// Example 2:
26+
//
27+
// Input: n = 37
28+
// Output: 1478
29+
// Explanation: There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
30+
// - 1 since 1 * 1 = 1.
31+
// - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
32+
// - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
33+
// - 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
34+
// Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
35+
//
36+
//  
37+
// Constraints:
38+
//
39+
// 1 <= n <= 1000
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
int punishmentNumber(int n) {
46+
int res = 0;
47+
for (int i = 1; i <= n; i++) {
48+
if (can_partition(to_string(i * i), 0, 0, i)) {
49+
res += i * i;
50+
}
51+
}
52+
return res;
53+
}
54+
55+
bool can_partition(string s, int index, int cur_sum, int target) {
56+
if (index == s.size()) {
57+
return cur_sum == target;
58+
}
59+
60+
int num = 0;
61+
for (int i = index; i < s.size(); i++) {
62+
num = num * 10 + s[i] - '0';
63+
if (num + cur_sum > target) {
64+
break;
65+
}
66+
67+
if (can_partition(s, i + 1, cur_sum + num, target)) {
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
73+
};
74+
75+
76+
class Solution {
77+
public:
78+
int punishmentNumber(int n) {
79+
int res = 0;
80+
for (int i = 1; i <= n; i++) {
81+
int sq = i * i;
82+
if (can_partition(sq, i, 0)) {
83+
res += sq;
84+
cout << i << endl;
85+
}
86+
}
87+
return res;
88+
}
89+
90+
bool can_partition(int num, int target, int cur_sum) {
91+
if (num == 0) {
92+
return cur_sum == target;
93+
}
94+
95+
int base = 1;
96+
while (num >= base) {
97+
int sum = num % (base * 10);
98+
if (cur_sum + sum > target) {
99+
break;
100+
}
101+
if (can_partition(num / (base * 10), target, cur_sum + sum)) {
102+
return true;
103+
}
104+
base *= 10;
105+
}
106+
return false;
107+
}
108+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Tag: Math, Backtracking
2+
# Time: O(N * 2^K)
3+
# Space: O(K)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/bLgWWGhaweM
7+
8+
# Given a positive integer n, return the punishment number of n.
9+
# The punishment number of n is defined as the sum of the squares of all integers i such that:
10+
#
11+
# 1 <= i <= n
12+
# The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.
13+
#
14+
#  
15+
# Example 1:
16+
#
17+
# Input: n = 10
18+
# Output: 182
19+
# Explanation: There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
20+
# - 1 since 1 * 1 = 1
21+
# - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
22+
# - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
23+
# Hence, the punishment number of 10 is 1 + 81 + 100 = 182
24+
#
25+
# Example 2:
26+
#
27+
# Input: n = 37
28+
# Output: 1478
29+
# Explanation: There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
30+
# - 1 since 1 * 1 = 1.
31+
# - 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
32+
# - 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
33+
# - 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
34+
# Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
35+
#
36+
#  
37+
# Constraints:
38+
#
39+
# 1 <= n <= 1000
40+
#
41+
#
42+
43+
class Solution:
44+
def punishmentNumber(self, n: int) -> int:
45+
res = 0
46+
for i in range(1, n + 1):
47+
if self.canPartition(str(i * i), 0, 0, i):
48+
res += i * i
49+
50+
return res
51+
52+
def canPartition(self, s: string, cur_sum: int, index: int, target: int) -> bool:
53+
if index == len(s):
54+
return cur_sum == target
55+
56+
num = 0
57+
for i in range(index, len(s)):
58+
num = num * 10 + int(s[i])
59+
if cur_sum + num > target:
60+
break
61+
62+
if self.canPartition(s, cur_sum + num, i + 1, target):
63+
return True
64+
return False
65+
66+
class Solution:
67+
def punishmentNumber(self, n: int) -> int:
68+
punishment_sum = 0
69+
for k in range(1, n + 1):
70+
k_squared = k * k
71+
if self.can_partition(k_squared, k, 0):
72+
punishment_sum += k_squared
73+
return punishment_sum
74+
75+
def can_partition(self, num: int, target: int, cur_sum: int) -> bool:
76+
if num == 0:
77+
return cur_sum == target
78+
79+
base = 1
80+
while num >= base:
81+
part = num % (base * 10) # 取出最低的几位数
82+
if cur_sum + part > target:
83+
break
84+
if self.can_partition(num // (base * 10), target, cur_sum + part):
85+
return True
86+
base *= 10 # 继续尝试更长的前缀
87+
return False

0 commit comments

Comments
 (0)
Please sign in to comment.