-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2698-find-the-punishment-number-of-an-integer.js
More file actions
45 lines (41 loc) · 1.14 KB
/
2698-find-the-punishment-number-of-an-integer.js
File metadata and controls
45 lines (41 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 2698. Find the Punishment Number of an Integer
* https://leetcode.com/problems/find-the-punishment-number-of-an-integer/
* Difficulty: Medium
*
* Given a positive integer n, return the punishment number of n.
*
* The punishment number of n is defined as the sum of the squares of all integers i such that:
* - 1 <= i <= n
* - 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.
*/
/**
* @param {number} n
* @return {number}
*/
var punishmentNumber = function(n) {
function canPartition(num, target, start) {
if (!target && start === num.length) {
return true;
} else if (start >= num.length) {
return false;
}
for (let i = start, sum = 0; i < num.length; i++) {
sum = sum * 10 + +num[i];
if (sum > target) {
break;
} else if (canPartition(num, target - sum, i + 1)) {
return true;
}
}
return false;
}
let result = 0;
for (let i = 1; i <= n; i++) {
if (canPartition((i * i).toString(), i, 0)) {
result += i * i;
}
}
return result;
};