Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #1320

Merged
merged 102 commits into from
Feb 15, 2025
Merged

Develop #1320

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
fe66429
Automated commit 1
mah-shamim Feb 15, 2025
01b7618
Automated commit rollback 1
mah-shamim Feb 15, 2025
d7fa5eb
Automated commit 2
mah-shamim Feb 15, 2025
9c276b6
Automated commit rollback 2
mah-shamim Feb 15, 2025
314779a
Automated commit 3
mah-shamim Feb 15, 2025
a93df88
Automated commit rollback 3
mah-shamim Feb 15, 2025
c353c74
Automated commit 4
mah-shamim Feb 15, 2025
3bc8041
Automated commit rollback 4
mah-shamim Feb 15, 2025
0b3511a
Automated commit 5
mah-shamim Feb 15, 2025
baea3c7
Automated commit rollback 5
mah-shamim Feb 15, 2025
5dac46f
Automated commit 6
mah-shamim Feb 15, 2025
36b4115
Automated commit rollback 6
mah-shamim Feb 15, 2025
005f774
Automated commit 7
mah-shamim Feb 15, 2025
545abc9
Automated commit rollback 7
mah-shamim Feb 15, 2025
6c1ea2c
Automated commit 8
mah-shamim Feb 15, 2025
4fa0a30
Automated commit rollback 8
mah-shamim Feb 15, 2025
6d9fd93
Automated commit 9
mah-shamim Feb 15, 2025
0d112a3
Automated commit rollback 9
mah-shamim Feb 15, 2025
55bbd7c
Automated commit 10
mah-shamim Feb 15, 2025
c2367f4
Automated commit rollback 10
mah-shamim Feb 15, 2025
aee3ba2
Automated commit 11
mah-shamim Feb 15, 2025
566ca21
Automated commit rollback 11
mah-shamim Feb 15, 2025
cc9ddb8
Automated commit 12
mah-shamim Feb 15, 2025
1e9c547
Automated commit rollback 12
mah-shamim Feb 15, 2025
9261b35
Automated commit 13
mah-shamim Feb 15, 2025
435e570
Automated commit rollback 13
mah-shamim Feb 15, 2025
5a6e956
Automated commit 14
mah-shamim Feb 15, 2025
8f25943
Automated commit rollback 14
mah-shamim Feb 15, 2025
84befcb
Automated commit 15
mah-shamim Feb 15, 2025
b9e10b0
Automated commit rollback 15
mah-shamim Feb 15, 2025
6ade761
Automated commit 16
mah-shamim Feb 15, 2025
c36706e
Automated commit rollback 16
mah-shamim Feb 15, 2025
864acc9
Automated commit 17
mah-shamim Feb 15, 2025
1ccc5ae
Automated commit rollback 17
mah-shamim Feb 15, 2025
832ca58
Automated commit 18
mah-shamim Feb 15, 2025
0b0e7fe
Automated commit rollback 18
mah-shamim Feb 15, 2025
181e472
Automated commit 19
mah-shamim Feb 15, 2025
53b3535
Automated commit rollback 19
mah-shamim Feb 15, 2025
b3c3dfe
Automated commit 20
mah-shamim Feb 15, 2025
3c1b7a2
Automated commit rollback 20
mah-shamim Feb 15, 2025
c73b897
Automated commit 21
mah-shamim Feb 15, 2025
5706c04
Automated commit rollback 21
mah-shamim Feb 15, 2025
b882482
Automated commit 22
mah-shamim Feb 15, 2025
4746e34
Automated commit rollback 22
mah-shamim Feb 15, 2025
e6c40bd
Automated commit 23
mah-shamim Feb 15, 2025
7c6157a
Automated commit rollback 23
mah-shamim Feb 15, 2025
562a807
Automated commit 24
mah-shamim Feb 15, 2025
7376c66
Automated commit rollback 24
mah-shamim Feb 15, 2025
1269b95
Automated commit 25
mah-shamim Feb 15, 2025
bd4534a
Automated commit rollback 25
mah-shamim Feb 15, 2025
bed20c5
Automated commit 26
mah-shamim Feb 15, 2025
a16a7cf
Automated commit rollback 26
mah-shamim Feb 15, 2025
98b7af3
Automated commit 27
mah-shamim Feb 15, 2025
f487355
Automated commit rollback 27
mah-shamim Feb 15, 2025
06329f6
Automated commit 28
mah-shamim Feb 15, 2025
1117499
Automated commit rollback 28
mah-shamim Feb 15, 2025
84d24c8
Automated commit 29
mah-shamim Feb 15, 2025
c31458c
Automated commit rollback 29
mah-shamim Feb 15, 2025
b38fafc
Automated commit 30
mah-shamim Feb 15, 2025
aca83c3
Automated commit rollback 30
mah-shamim Feb 15, 2025
a8f0eb5
Automated commit 31
mah-shamim Feb 15, 2025
f36dfe7
Automated commit rollback 31
mah-shamim Feb 15, 2025
e244b99
Automated commit 32
mah-shamim Feb 15, 2025
a1be881
Automated commit rollback 32
mah-shamim Feb 15, 2025
b1efa7c
Automated commit 33
mah-shamim Feb 15, 2025
416b338
Automated commit rollback 33
mah-shamim Feb 15, 2025
0c21fac
Automated commit 34
mah-shamim Feb 15, 2025
b048d82
Automated commit rollback 34
mah-shamim Feb 15, 2025
155d1dd
Automated commit 35
mah-shamim Feb 15, 2025
5d2a7b8
Automated commit rollback 35
mah-shamim Feb 15, 2025
796ed61
Automated commit 36
mah-shamim Feb 15, 2025
9bbf6fd
Automated commit rollback 36
mah-shamim Feb 15, 2025
94207e3
Automated commit 37
mah-shamim Feb 15, 2025
2c87c16
Automated commit rollback 37
mah-shamim Feb 15, 2025
1a324e9
Automated commit 38
mah-shamim Feb 15, 2025
9f6fa74
Automated commit rollback 38
mah-shamim Feb 15, 2025
39ddfbd
Automated commit 39
mah-shamim Feb 15, 2025
c286813
Automated commit rollback 39
mah-shamim Feb 15, 2025
3d4341f
Automated commit 40
mah-shamim Feb 15, 2025
d4d54dc
Automated commit rollback 40
mah-shamim Feb 15, 2025
c91314e
Automated commit 41
mah-shamim Feb 15, 2025
7766358
Automated commit rollback 41
mah-shamim Feb 15, 2025
0aa40b6
Automated commit 42
mah-shamim Feb 15, 2025
5819497
Automated commit rollback 42
mah-shamim Feb 15, 2025
e1878e1
Automated commit 43
mah-shamim Feb 15, 2025
037653e
Automated commit rollback 43
mah-shamim Feb 15, 2025
388f022
Automated commit 44
mah-shamim Feb 15, 2025
c88f833
Automated commit rollback 44
mah-shamim Feb 15, 2025
eb66b90
Automated commit 45
mah-shamim Feb 15, 2025
2afe75e
Automated commit rollback 45
mah-shamim Feb 15, 2025
c86dd67
Automated commit 46
mah-shamim Feb 15, 2025
dd9316a
Automated commit rollback 46
mah-shamim Feb 15, 2025
40b2a8c
Automated commit 47
mah-shamim Feb 15, 2025
be744e6
Automated commit rollback 47
mah-shamim Feb 15, 2025
3dc8aa0
Automated commit 48
mah-shamim Feb 15, 2025
4531ca5
Automated commit rollback 48
mah-shamim Feb 15, 2025
ca35982
Automated commit 49
mah-shamim Feb 15, 2025
d6aabc9
Automated commit rollback 49
mah-shamim Feb 15, 2025
ab1fc95
Automated commit 50
mah-shamim Feb 15, 2025
99bccc9
Automated commit rollback 50
mah-shamim Feb 15, 2025
a44ed9a
#1317, #1318 leetcode problems 002698-find-the-punishment-number-of-a…
mah-shamim Feb 15, 2025
5345d22
Merge pull request #1319 from mah-shamim/002698-find-the-punishment-n…
mah-shamim Feb 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions algorithms/002698-find-the-punishment-number-of-an-integer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
2698\. Find the Punishment Number of an Integer

**Difficulty:** Medium

**Topics:** `Math`, `Backtracking`

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`.


**Example 1:**

- **Input:** n = 10
- **Output:** 182
- **Explanation:** There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
Hence, the punishment number of 10 is 1 + 81 + 100 = 182

**Example 2:**

- **Input:** n = 37
- **Output:** 1478
- **Explanation:** There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1.
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478



**Constraints:**
- `1 <= n <= 1000`

**Hint:**
1. Can we generate all possible partitions of a number?
2. Use a recursive algorithm that splits the number into two parts, generates all possible partitions of each part recursively, and then combines them in all possible ways.



**Solution:**

We need to determine the "punishment number" of a given integer `n`. The punishment number is defined as the sum of the squares of all integers `i` (where `1 <= i <= n`) such that the decimal representation of `i * i` can be partitioned into contiguous substrings whose sum equals `i`.

### Approach
1. **Iterate through each integer `i` from 1 to `n`**: For each integer, compute its square and convert the square into a string.
2. **Check if the square can be partitioned**: For each integer `i`, check if its square can be split into contiguous substrings such that the sum of these substrings equals `i`. This check is performed using a recursive helper function.
3. **Recursive helper function**: This function attempts to split the string representation of the square into all possible contiguous parts, checking if any combination of these parts sums to `i`. If a valid partition is found, the integer's square is added to the total sum.

Let's implement this solution in PHP: **[2698. Find the Punishment Number of an Integer](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002698-find-the-punishment-number-of-an-integer/solution.php)**

```php
<?php
/**
* @param Integer $n
* @return Integer
*/
function punishmentNumber($n) {
...
...
...
/**
* go to ./solution.php
*/
}

/**
* @param $s
* @param $target
* @param $index
* @param $currentSum
* @return bool
*/
function canSplit($s, $target, $index, $currentSum) {
...
...
...
/**
* go to ./solution.php
*/
}

// Example Test Cases
echo punishmentNumber(10) . "\n"; // Output: 182
echo punishmentNumber(37) . "\n"; // Output: 1478
?>
```

### Explanation:

1. **Main Loop**: The main loop iterates through each integer `i` from 1 to `n`. For each integer, it computes the square and converts it to a string.
2. **Recursive Check**: The `canSplit` function is a recursive helper that checks if the string representation of the square can be partitioned into contiguous substrings that sum to `i`. It starts from the beginning of the string and tries all possible splits, checking if any combination of splits results in the sum equal to `i`.
3. **Early Termination**: If adding a part of the string exceeds the target sum `i`, the loop breaks early to avoid unnecessary computations.
4. **Sum Calculation**: If a valid partition is found for an integer `i`, its square is added to the total punishment sum.

This approach efficiently checks all possible partitions using recursion with backtracking, ensuring that we only consider valid splits and sum calculations, leading to the correct result.

**Contact Links**

If you found this series helpful, please consider giving the **[repository](https://github.com/mah-shamim/leet-code-in-php)** a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

- **[LinkedIn](https://www.linkedin.com/in/arifulhaque/)**
- **[GitHub](https://github.com/mah-shamim)**
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class Solution {

/**
* @param Integer $n
* @return Integer
*/
function punishmentNumber($n) {
$sum = 0;
for ($i = 1; $i <= $n; $i++) {
$square = $i * $i;
$s = strval($square);
if ($this->canSplit($s, $i, 0, 0)) {
$sum += $square;
}
}
return $sum;
}

/**
* @param $s
* @param $target
* @param $index
* @param $currentSum
* @return bool
*/
function canSplit($s, $target, $index, $currentSum) {
$len = strlen($s);
if ($index == $len) {
return $currentSum == $target;
}
for ($splitLength = 1; $splitLength <= $len - $index; $splitLength++) {
$numStr = substr($s, $index, $splitLength);
$num = intval($numStr);
if ($currentSum + $num > $target) {
break;
}
if ($this->canSplit($s, $target, $index + $splitLength, $currentSum + $num)) {
return true;
}
}
return false;
}
}