You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[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\)| - |
115
116
|[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\)| - |
116
117
|[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\)| - |
117
118
|[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\)| - |
|[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\)| - |
// 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
+
classSolution {
44
+
public:
45
+
intpunishmentNumber(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
+
boolcan_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
+
returntrue;
69
+
}
70
+
}
71
+
returnfalse;
72
+
}
73
+
};
74
+
75
+
76
+
classSolution {
77
+
public:
78
+
intpunishmentNumber(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
+
boolcan_partition(int num, int target, int cur_sum) {
# 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
0 commit comments