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 #1169

Merged

Develop #1169

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
10000 commits
Select commit Hold shift + click to select a range
9bfdb54
Automated commit 35
mah-shamim Jan 15, 2025
ac775d0
Automated commit rollback 35
mah-shamim Jan 15, 2025
c18c52b
Automated commit 36
mah-shamim Jan 15, 2025
9c98250
Automated commit rollback 36
mah-shamim Jan 15, 2025
6492e57
Automated commit 37
mah-shamim Jan 15, 2025
a0b21d4
Automated commit rollback 37
mah-shamim Jan 15, 2025
23f75f8
Automated commit 38
mah-shamim Jan 15, 2025
aaed19f
Automated commit rollback 38
mah-shamim Jan 15, 2025
455b09d
Automated commit 39
mah-shamim Jan 15, 2025
4eda0ef
Automated commit rollback 39
mah-shamim Jan 15, 2025
6d60f3d
Automated commit 40
mah-shamim Jan 15, 2025
5f7a10f
Automated commit rollback 40
mah-shamim Jan 15, 2025
d1ea5b5
Automated commit 41
mah-shamim Jan 15, 2025
0710e62
Automated commit rollback 41
mah-shamim Jan 15, 2025
88e04ca
Automated commit 42
mah-shamim Jan 15, 2025
e49f91f
Automated commit rollback 42
mah-shamim Jan 15, 2025
e776d8b
Automated commit 43
mah-shamim Jan 15, 2025
d4b9719
Automated commit rollback 43
mah-shamim Jan 15, 2025
0b6a30a
Automated commit 44
mah-shamim Jan 15, 2025
e9c8ac9
Automated commit rollback 44
mah-shamim Jan 15, 2025
4634241
Automated commit 45
mah-shamim Jan 15, 2025
5da7617
Automated commit rollback 45
mah-shamim Jan 15, 2025
fd1e457
Automated commit 46
mah-shamim Jan 15, 2025
f02a8ba
Automated commit rollback 46
mah-shamim Jan 15, 2025
58eca8d
Automated commit 47
mah-shamim Jan 15, 2025
27c689e
Automated commit rollback 47
mah-shamim Jan 15, 2025
1a148fc
Automated commit 48
mah-shamim Jan 15, 2025
001f9a2
Automated commit rollback 48
mah-shamim Jan 15, 2025
5d34ff3
Automated commit 49
mah-shamim Jan 15, 2025
7fc4160
Automated commit rollback 49
mah-shamim Jan 15, 2025
0d5b40c
Automated commit 50
mah-shamim Jan 15, 2025
4404cfc
Automated commit rollback 50
mah-shamim Jan 15, 2025
7ba050d
#1149, #1150 leetcode problems 002429-minimize-xor submissions 150942…
mah-shamim Jan 15, 2025
86571bf
Merge pull request #1151 from mah-shamim/002429-minimize-xor
mah-shamim Jan 15, 2025
051af09
Merge pull request #1152 from mah-shamim/develop
mah-shamim Jan 15, 2025
b4695f6
#261, #262 leetcode problems 001249-minimum-remove-to-make-valid-pare…
mah-shamim Jan 15, 2025
a09c9fb
Merge pull request #1153 from mah-shamim/001249-minimum-remove-to-mak…
mah-shamim Jan 15, 2025
f471bbb
Merge pull request #1154 from mah-shamim/develop
mah-shamim Jan 15, 2025
b2248bb
Automated commit 1
mah-shamim Jan 16, 2025
fd558e2
Automated commit rollback 1
mah-shamim Jan 16, 2025
8454689
Automated commit 2
mah-shamim Jan 16, 2025
10286af
Automated commit rollback 2
mah-shamim Jan 16, 2025
3fd11a8
Automated commit 3
mah-shamim Jan 16, 2025
973d9da
Automated commit rollback 3
mah-shamim Jan 16, 2025
0d4aa64
Automated commit 4
mah-shamim Jan 16, 2025
f84e398
Automated commit rollback 4
mah-shamim Jan 16, 2025
03ec393
Automated commit 5
mah-shamim Jan 16, 2025
d12af46
Automated commit rollback 5
mah-shamim Jan 16, 2025
b7fe0c1
Automated commit 6
mah-shamim Jan 16, 2025
361ce7b
Automated commit rollback 6
mah-shamim Jan 16, 2025
ec3a69d
Automated commit 7
mah-shamim Jan 16, 2025
e84fd4c
Automated commit rollback 7
mah-shamim Jan 16, 2025
4deacea
Automated commit 8
mah-shamim Jan 16, 2025
cc6747c
Automated commit rollback 8
mah-shamim Jan 16, 2025
d31796b
Automated commit 9
mah-shamim Jan 16, 2025
9862f39
Automated commit rollback 9
mah-shamim Jan 16, 2025
a97d381
Automated commit 10
mah-shamim Jan 16, 2025
aa572f8
Automated commit rollback 10
mah-shamim Jan 16, 2025
8163275
Automated commit 11
mah-shamim Jan 16, 2025
a736071
Automated commit rollback 11
mah-shamim Jan 16, 2025
0cc7a44
Automated commit 12
mah-shamim Jan 16, 2025
8de0416
Automated commit rollback 12
mah-shamim Jan 16, 2025
7fd9807
Automated commit 13
mah-shamim Jan 16, 2025
4c2df37
Automated commit rollback 13
mah-shamim Jan 16, 2025
0608499
Automated commit 14
mah-shamim Jan 16, 2025
ea0b309
Automated commit rollback 14
mah-shamim Jan 16, 2025
710c06f
Automated commit 15
mah-shamim Jan 16, 2025
7f26690
Automated commit rollback 15
mah-shamim Jan 16, 2025
7921729
Automated commit 16
mah-shamim Jan 16, 2025
1a1dc5a
Automated commit rollback 16
mah-shamim Jan 16, 2025
46b378a
Automated commit 17
mah-shamim Jan 16, 2025
d358088
Automated commit rollback 17
mah-shamim Jan 16, 2025
49adc4e
Automated commit 18
mah-shamim Jan 16, 2025
924ae6b
Automated commit rollback 18
mah-shamim Jan 16, 2025
4972c7e
Automated commit 19
mah-shamim Jan 16, 2025
ba3d19f
Automated commit rollback 19
mah-shamim Jan 16, 2025
4c1e756
Automated commit 20
mah-shamim Jan 16, 2025
2270d37
Automated commit rollback 20
mah-shamim Jan 16, 2025
5b43f45
Automated commit 21
mah-shamim Jan 16, 2025
b89cb20
Automated commit rollback 21
mah-shamim Jan 16, 2025
8865ed4
Automated commit 22
mah-shamim Jan 16, 2025
4b47bf6
Automated commit rollback 22
mah-shamim Jan 16, 2025
87ffd30
Automated commit 23
mah-shamim Jan 16, 2025
579a3a2
Automated commit rollback 23
mah-shamim Jan 16, 2025
7d19ad4
Automated commit 24
mah-shamim Jan 16, 2025
e1103b2
Automated commit rollback 24
mah-shamim Jan 16, 2025
ca3298e
Automated commit 25
mah-shamim Jan 16, 2025
b47c7d0
Automated commit rollback 25
mah-shamim Jan 16, 2025
936d9c3
Automated commit 26
mah-shamim Jan 16, 2025
cfceb9e
Automated commit rollback 26
mah-shamim Jan 16, 2025
77db582
Automated commit 27
mah-shamim Jan 16, 2025
0bbc825
Automated commit rollback 27
mah-shamim Jan 16, 2025
258d859
Automated commit 28
mah-shamim Jan 16, 2025
b89d8c3
Automated commit rollback 28
mah-shamim Jan 16, 2025
0241c2a
Automated commit 29
mah-shamim Jan 16, 2025
45bf2e6
Automated commit rollback 29
mah-shamim Jan 16, 2025
1ab5998
Automated commit 30
mah-shamim Jan 16, 2025
af2b7c0
Automated commit rollback 30
mah-shamim Jan 16, 2025
0a03c4d
Automated commit 31
mah-shamim Jan 16, 2025
26ae790
Automated commit rollback 31
mah-shamim Jan 16, 2025
041f130
Automated commit 32
mah-shamim Jan 16, 2025
ecfc298
Automated commit rollback 32
mah-shamim Jan 16, 2025
5971276
Automated commit 33
mah-shamim Jan 16, 2025
27ea85b
Automated commit rollback 33
mah-shamim Jan 16, 2025
5806dec
Automated commit 34
mah-shamim Jan 16, 2025
c8db665
Automated commit rollback 34
mah-shamim Jan 16, 2025
d883b38
Automated commit 35
mah-shamim Jan 16, 2025
9d312f9
Automated commit rollback 35
mah-shamim Jan 16, 2025
411fabb
Automated commit 36
mah-shamim Jan 16, 2025
51eae11
Automated commit rollback 36
mah-shamim Jan 16, 2025
ab512ac
Automated commit 37
mah-shamim Jan 16, 2025
d2f18db
Automated commit rollback 37
mah-shamim Jan 16, 2025
1c9a311
Automated commit 38
mah-shamim Jan 16, 2025
a938b0c
Automated commit rollback 38
mah-shamim Jan 16, 2025
70978e1
Automated commit 39
mah-shamim Jan 16, 2025
bcd7d5d
Automated commit rollback 39
mah-shamim Jan 16, 2025
27b7f2b
Automated commit 40
mah-shamim Jan 16, 2025
b8d9dad
Automated commit rollback 40
mah-shamim Jan 16, 2025
422082c
Automated commit 41
mah-shamim Jan 16, 2025
3bc8adc
Automated commit rollback 41
mah-shamim Jan 16, 2025
f9698c9
Automated commit 42
mah-shamim Jan 16, 2025
a527d62
Automated commit rollback 42
mah-shamim Jan 16, 2025
779276d
Automated commit 43
mah-shamim Jan 16, 2025
cba1a24
Automated commit rollback 43
mah-shamim Jan 16, 2025
8ffd99d
Automated commit 44
mah-shamim Jan 16, 2025
a116957
Automated commit rollback 44
mah-shamim Jan 16, 2025
e391ca5
Automated commit 45
mah-shamim Jan 16, 2025
e196306
Automated commit rollback 45
mah-shamim Jan 16, 2025
0b42e4d
Automated commit 46
mah-shamim Jan 16, 2025
165005e
Automated commit rollback 46
mah-shamim Jan 16, 2025
d118df1
Automated commit 47
mah-shamim Jan 16, 2025
4e20ebf
Automated commit rollback 47
mah-shamim Jan 16, 2025
5de4811
Automated commit 48
mah-shamim Jan 16, 2025
77d913f
Automated commit rollback 48
mah-shamim Jan 16, 2025
dbeaa3f
Automated commit 49
mah-shamim Jan 16, 2025
eae99e5
Automated commit rollback 49
mah-shamim Jan 16, 2025
5a3ff40
Automated commit 50
mah-shamim Jan 16, 2025
41c6cb6
Automated commit rollback 50
mah-shamim Jan 16, 2025
66f2ed5
#1155, #1156 leetcode problems 002425-bitwise-xor-of-all-pairings sub…
mah-shamim Jan 16, 2025
070bd4d
Merge pull request #1157 from mah-shamim/002425-bitwise-xor-of-all-pa…
mah-shamim Jan 16, 2025
315a52b
Merge pull request #1158 from mah-shamim/develop
mah-shamim Jan 16, 2025
64fd7c8
#263, #264 leetcode problems 001255-maximum-score-words-formed-by-let…
mah-shamim Jan 16, 2025
7a8236b
Merge pull request #1159 from mah-shamim/001255-maximum-score-words-f…
mah-shamim Jan 16, 2025
4d9ce5f
Merge pull request #1160 from mah-shamim/develop
mah-shamim Jan 16, 2025
1bfd40a
Automated commit 1
mah-shamim Jan 17, 2025
4008da5
Automated commit rollback 1
mah-shamim Jan 17, 2025
d004723
Automated commit 2
mah-shamim Jan 17, 2025
0d7721e
Automated commit rollback 2
mah-shamim Jan 17, 2025
f7d207b
Automated commit 3
mah-shamim Jan 17, 2025
1cd68f9
Automated commit rollback 3
mah-shamim Jan 17, 2025
81099a0
Automated commit 4
mah-shamim Jan 17, 2025
c6dfd24
Automated commit rollback 4
mah-shamim Jan 17, 2025
80d51af
Automated commit 5
mah-shamim Jan 17, 2025
8934936
Automated commit rollback 5
mah-shamim Jan 17, 2025
17fcc54
Automated commit 6
mah-shamim Jan 17, 2025
8fac18e
Automated commit rollback 6
mah-shamim Jan 17, 2025
9494d6d
Automated commit 7
mah-shamim Jan 17, 2025
06836ea
Automated commit rollback 7
mah-shamim Jan 17, 2025
c4194c6
Automated commit 8
mah-shamim Jan 17, 2025
750a653
Automated commit rollback 8
mah-shamim Jan 17, 2025
32a5acb
Automated commit 9
mah-shamim Jan 17, 2025
9b863f2
Automated commit rollback 9
mah-shamim Jan 17, 2025
3824e55
Automated commit 10
mah-shamim Jan 17, 2025
e99fbaa
Automated commit rollback 10
mah-shamim Jan 17, 2025
a3de391
Automated commit 11
mah-shamim Jan 17, 2025
4f06738
Automated commit rollback 11
mah-shamim Jan 17, 2025
75ccf74
Automated commit 12
mah-shamim Jan 17, 2025
4db1e25
Automated commit rollback 12
mah-shamim Jan 17, 2025
d5a443f
Automated commit 13
mah-shamim Jan 17, 2025
760a517
Automated commit rollback 13
mah-shamim Jan 17, 2025
c5303eb
Automated commit 14
mah-shamim Jan 17, 2025
579a1a7
Automated commit rollback 14
mah-shamim Jan 17, 2025
1aa4fb4
Automated commit 15
mah-shamim Jan 17, 2025
12a850e
Automated commit rollback 15
mah-shamim Jan 17, 2025
45c691f
Automated commit 16
mah-shamim Jan 17, 2025
d782b55
Automated commit rollback 16
mah-shamim Jan 17, 2025
b1c5eb9
Automated commit 17
mah-shamim Jan 17, 2025
a93d05b
Automated commit rollback 17
mah-shamim Jan 17, 2025
5726dc9
Automated commit 18
mah-shamim Jan 17, 2025
61529a4
Automated commit rollback 18
mah-shamim Jan 17, 2025
e4af797
Automated commit 19
mah-shamim Jan 17, 2025
da18fda
Automated commit rollback 19
mah-shamim Jan 17, 2025
358bece
Automated commit 20
mah-shamim Jan 17, 2025
69fe5ad
Automated commit rollback 20
mah-shamim Jan 17, 2025
ea99463
Automated commit 21
mah-shamim Jan 17, 2025
9bf8d04
Automated commit rollback 21
mah-shamim Jan 17, 2025
d874fe5
Automated commit 22
mah-shamim Jan 17, 2025
a140e1a
Automated commit rollback 22
mah-shamim Jan 17, 2025
803916b
Automated commit 23
mah-shamim Jan 17, 2025
0a75f61
Automated commit rollback 23
mah-shamim Jan 17, 2025
c3108e9
Automated commit 24
mah-shamim Jan 17, 2025
944e60f
Automated commit rollback 24
mah-shamim Jan 17, 2025
c445ae8
Automated commit 25
mah-shamim Jan 17, 2025
4026a78
Automated commit rollback 25
mah-shamim Jan 17, 2025
0ad8d0e
Automated commit 26
mah-shamim Jan 17, 2025
318f2de
Automated commit rollback 26
mah-shamim Jan 17, 2025
65433ea
Automated commit 27
mah-shamim Jan 17, 2025
1b740fe
Automated commit rollback 27
mah-shamim Jan 17, 2025
3f2314c
Automated commit 28
mah-shamim Jan 17, 2025
dd9c4d6
Automated commit rollback 28
mah-shamim Jan 17, 2025
a95afa5
Automated commit 29
mah-shamim Jan 17, 2025
cdab9a6
Automated commit rollback 29
mah-shamim Jan 17, 2025
c3b6b3f
Automated commit 30
mah-shamim Jan 17, 2025
0d0d402
Automated commit rollback 30
mah-shamim Jan 17, 2025
fc58984
Automated commit 31
mah-shamim Jan 17, 2025
b86f706
Automated commit rollback 31
mah-shamim Jan 17, 2025
1f19fa6
Automated commit 32
mah-shamim Jan 17, 2025
3e637ae
Automated commit rollback 32
mah-shamim Jan 17, 2025
ab40ce2
Automated commit 33
mah-shamim Jan 17, 2025
7cb3824
Automated commit rollback 33
mah-shamim Jan 17, 2025
d9466f4
Automated commit 34
mah-shamim Jan 17, 2025
fc14390
Automated commit rollback 34
mah-shamim Jan 17, 2025
a48ffd2
Automated commit 35
mah-shamim Jan 17, 2025
9f3e338
Automated commit rollback 35
mah-shamim Jan 17, 2025
91df2c6
Automated commit 36
mah-shamim Jan 17, 2025
cf9c3de
Automated commit rollback 36
mah-shamim Jan 17, 2025
d647cea
Automated commit 37
mah-shamim Jan 17, 2025
0b56c63
Automated commit rollback 37
mah-shamim Jan 17, 2025
e237e3f
Automated commit 38
mah-shamim Jan 17, 2025
e1d61df
Automated commit rollback 38
mah-shamim Jan 17, 2025
619aaaa
Automated commit 39
mah-shamim Jan 17, 2025
2ae2998
Automated commit rollback 39
mah-shamim Jan 17, 2025
fe1c062
Automated commit 40
mah-shamim Jan 17, 2025
0249df5
Automated commit rollback 40
mah-shamim Jan 17, 2025
ad17c72
Automated commit 41
mah-shamim Jan 17, 2025
3d28b2a
Automated commit rollback 41
mah-shamim Jan 17, 2025
75d7fcd
Automated commit 42
mah-shamim Jan 17, 2025
85abe04
Automated commit rollback 42
mah-shamim Jan 17, 2025
4427189
Automated commit 43
mah-shamim Jan 17, 2025
73a3aa0
Automated commit rollback 43
mah-shamim Jan 17, 2025
80989a7
Automated commit 44
mah-shamim Jan 17, 2025
856ce72
Automated commit rollback 44
mah-shamim Jan 17, 2025
3f12ebb
Automated commit 45
mah-shamim Jan 17, 2025
d59e891
Automated commit rollback 45
mah-shamim Jan 17, 2025
bc0b203
Automated commit 46
mah-shamim Jan 17, 2025
b2aa9d0
Automated commit rollback 46
mah-shamim Jan 17, 2025
0d2b83c
Automated commit 47
mah-shamim Jan 17, 2025
5b315ca
Automated commit rollback 47
mah-shamim Jan 17, 2025
f6292f1
Automated commit 48
mah-shamim Jan 17, 2025
94a538e
Automated commit rollback 48
mah-shamim Jan 17, 2025
00511f3
Automated commit 49
mah-shamim Jan 17, 2025
7f995f4
Automated commit rollback 49
mah-shamim Jan 17, 2025
acfa13a
Automated commit 50
mah-shamim Jan 17, 2025
86252ff
Automated commit rollback 50
mah-shamim Jan 17, 2025
df2d63a
#1162, #1163 leetcode problems 002683-neighboring-bitwise-xor submiss…
mah-shamim Jan 17, 2025
03ad885
Merge pull request #1164 from mah-shamim/002683-neighboring-bitwise-xor
mah-shamim Jan 17, 2025
f2624dd
Merge pull request #1165 from mah-shamim/develop
mah-shamim Jan 17, 2025
731efce
#265, #266 leetcode problems 001289-minimum-falling-path-sum-ii submi…
mah-shamim Jan 17, 2025
9784526
Merge pull request #1166 from mah-shamim/001289-minimum-falling-path-…
mah-shamim Jan 17, 2025
3e80048
Merge pull request #1167 from mah-shamim/develop
mah-shamim Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Custom issue template
about: Describe this issue template's purpose here.
title: ''
labels: ''
assignees: ''

---


2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
labels: question
assignees: ''

---
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
.vscode/

automate_git.bat
auto_commit.sh
auto_commit.bat
*.iml
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
Php-based LeetCode algorithm problem solutions, regularly updated.


![Alt](https://repobeats.axiom.co/api/embed/9f698d3cdb218feb946714e458ac140a276b39a5.svg "Repobeats analytics image")
![Repobeats analytics image](https://repobeats.axiom.co/api/embed/9f698d3cdb218feb946714e458ac140a276b39a5.svg "Repobeats analytics image")



56 changes: 56 additions & 0 deletions algorithms/000040-combination-sum-ii/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
40\. Combination Sum II

**Difficulty:** Medium

**Topics:** `Array`, `Backtracking`

Given a collection of candidate numbers (`candidates`) and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sum to `target`.

Each number in `candidates` may only be used **once** in the combination.

**Note:** The solution set must not contain duplicate combinations.

**Example 1:**

- **Input:** candidates = [10,1,2,7,6,1,5], target = 8
- **Output:** [[1,1,6], [1,2,5], [1,7], [2,6]]

**Example 2:**

- **Input:** candidates = [2,5,2,1,2], target = 5
- **Output:** [[1,2,2], [5]]

**Constraints:**

- <code>1 <= candidates.length <= 100</code>
- <code>1 <= candidates[i] <= 50</code>
- <code>1 <= target <= 30</code>


**Solution:**


We can use a backtracking approach. The key idea is to sort the array first to handle duplicates easily and then explore all possible combinations using backtracking.

Let's implement this solution in PHP: **[40. Combination Sum II](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000040-combination-sum-ii/solution.php)**

### Explanation:

1. **Sorting**: The candidates array is sorted to handle duplicates easily and to ensure combinations are formed in a sorted order.
2. **Backtracking**: The `backtrack` function is used to explore all possible combinations.
- If the `target` becomes zero, we add the current combination to the result.
- We iterate over the candidates starting from the current index. If a candidate is the same as the previous one, we skip it to avoid duplicate combinations.
- We subtract the current candidate from the target and recursively call the `backtrack` function with the new target and the next index.
- The recursive call continues until we either find a valid combination or exhaust all possibilities.
3. **Pruning**: If the candidate exceeds the target, we break out of the loop early since further candidates will also exceed the target.

This code will output all unique combinations that sum up to the target while ensuring that each candidate is used only once in each combination.

**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)**
44 changes: 44 additions & 0 deletions algorithms/000040-combination-sum-ii/solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class Solution {

/**
* @param Integer[] $candidates
* @param Integer $target
* @return Integer[][]
*/
function combinationSum2($candidates, $target) {
sort($candidates);
$result = [];
$this->backtrack($candidates, $target, 0, [], $result);
return $result;
}

/**
* @param $candidates
* @param $target
* @param $start
* @param $currentCombination
* @param $result
* @return void
*/
function backtrack($candidates, $target, $start, $currentCombination, &$result) {
if ($target == 0) {
$result[] = $currentCombination;
return;
}

for ($i = $start; $i < count($candidates); $i++) {
if ($i > $start && $candidates[$i] == $candidates[$i - 1]) {
continue; // Skip duplicates
}

if ($candidates[$i] > $target) {
break; // No need to continue if the candidate exceeds the target
}

self::backtrack($candidates, $target - $candidates[$i], $i + 1, array_merge($currentCombination, [$candidates[$i]]), $result);
}
}

}
81 changes: 80 additions & 1 deletion algorithms/000075-sort-colors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Medium

**Topics:** `Array`, `Two Pointers`, `Sorting`

Given an array `nums` with `n` objects colored red, white, or blue, sort them [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers `0`, `1`, and `2` to represent the color red, white, and blue, respectively.
Expand All @@ -24,4 +26,81 @@ You must solve this problem without using the library's sort function.
- <code>1 <= n <= 300</code>
- `nums[i]` is either `0`, `1`, or `2`.

**Follow-up:** Could you come up with a one-pass algorithm using only constant extra space?
**Follow-up:** Could you come up with a one-pass algorithm using only constant extra space?

**Hint:**
1. A rather straight forward solution is a two-pass algorithm using counting sort.
2. Iterate the array counting number of 0's, 1's, and 2's.
3. Overwrite array with the total number of 0's, then 1's and followed by 2's.


**Solution:**


To solve this problem, we can follow these steps:

The goal is to sort the array `nums` with elements representing the colors red (0), white (1), and blue (2) in one pass, using constant extra space.

### Approach:

The most efficient way to solve this problem is by using the Dutch National Flag algorithm, which is a one-pass algorithm with constant extra space. The idea is to use three pointers:
- `low` to track the position for the next 0 (red),
- `mid` to traverse the array,
- `high` to track the position for the next 2 (blue).

### Algorithm:
1. Initialize three pointers:
- `low` at the start (0),
- `mid` at the start (0),
- `high` at the end (n-1) of the array.

2. Traverse the array with `mid`:
- If `nums[mid]` is `0` (red), swap `nums[mid]` with `nums[low]`, then increment both `low` and `mid`.
- If `nums[mid]` is `1` (white), move `mid` to the next element.
- If `nums[mid]` is `2` (blue), swap `nums[mid]` with `nums[high]` and decrement `high`.

3. Continue until `mid` exceeds `high`.

This algorithm sorts the array in-place with a single pass.

Let's implement this solution in PHP: **[75. Sort Colors](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000075-sort-colors/solution.php)**

```php
<?php
// Example usage:
$nums1 = [2, 0, 2, 1, 1, 0];
sortColors($nums1);
print_r($nums1); // Output: [0, 0, 1, 1, 2, 2]

$nums2 = [2,0,1];
sortColors($nums2);
print_r($nums2); // Output: [0,1,2]

?>
```

### Explanation:

1. **Initialization:**
- `low` starts at the beginning (`0`).
- `mid` starts at the beginning (`0`).
- `high` starts at the end (`n-1`).

2. **Loop Through Array:**
- If `nums[mid]` is `0`, it swaps with `nums[low]` because `0` should be at the front. Both pointers `low` and `mid` are then incremented.
- If `nums[mid]` is `1`, it is already in the correct position, so only `mid` is incremented.
- If `nums[mid]` is `2`, it swaps with `nums[high]` because `2` should be at the end. Only `high` is decremented, while `mid` stays the same to check the swapped element.

3. **Completion:**
- The loop continues until `mid` passes `high`, ensuring that the array is sorted in the order of `0`, `1`, and `2`.

This approach is optimal with O(n) time complexity and O(1) space complexity.

**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)**
37 changes: 22 additions & 15 deletions algorithms/000075-sort-colors/solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ class Solution {
* @param Integer[] $nums
* @return NULL
*/
function sortColors(array &$nums) {
$l = 0; // The next 0 should be placed in l.
$r = count($nums) - 1; // The next 2 should be placed in r.
function sortColors(&$nums) {
$low = 0;
$mid = 0;
$high = count($nums) - 1;

for ($i = 0; $i <= $r;) {
if ($nums[$i] == 0) {
list($nums[$i], $nums[$l]) = array($nums[$l], $nums[$i]);
$i++;
$l++;
} elseif ($nums[$i] == 1) {
$i++;
} else {
// We may swap a 0 to index i, but we're still not sure whether this 0
// is placed in the correct index, so we can't move pointer i.
list($nums[$i], $nums[$r]) = array($nums[$r], $nums[$i]);
$r--;
while ($mid <= $high) {
if ($nums[$mid] == 0) {
// Swap nums[low] and nums[mid]
$temp = $nums[$low];
$nums[$low] = $nums[$mid];
$nums[$mid] = $temp;

$low++;
$mid++;
} elseif ($nums[$mid] == 1) {
$mid++;
} else { // nums[$mid] == 2
// Swap nums[mid] and nums[high]
$temp = $nums[$high];
$nums[$high] = $nums[$mid];
$nums[$mid] = $temp;

$high--;
}
}
}
Expand Down
84 changes: 82 additions & 2 deletions algorithms/000078-subsets/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
78\. Subsets

Medium
**Difficulty:** Medium

**Topics:** `Array`, `Backtracking`, `Bit Manipulation`

Given an integer array `nums` of **unique** elements, return _all possible subsets[^1] (the power set)._

Expand All @@ -23,4 +25,82 @@ The solution set **must not** contain duplicate subsets. Return the solution in
- <code>-10 <= nums[i] <= 10</code>
- All the numbers of `nums` are **unique**.

[^1]: **Subset** <code>A **subset** of an array is a selection of elements (possibly none) of the array.</code>
[^1]: **Subset** <code>A **subset** of an array is a selection of elements (possibly none) of the array.</code>

**Solution:**


To solve this problem, we can follow these steps:

Let's implement this solution in PHP: **[78. Subsets](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000078-subsets/solution.php)**

```php
<?php
// Test the function with example inputs
print_r(subsets([1,2,3])); // Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
print_r(subsets([0])); // Output: [[],[0]]
?>
```

### Explanation:

1. **Function Signature and Input**:
- The function `subsets()` takes an array of integers `nums` as input and returns an array of arrays, where each inner array represents a subset of `nums`.
- The return type is `Integer[][]`, meaning an array of arrays of integers.

2. **Initialization**:
- `$ans = [[]];`: Initialize the result array `$ans` with an empty subset (`[]`). This is the base case where the subset is empty.

3. **Iterating Over Each Number**:
- `foreach($nums as $num)`: Loop through each number in the input array `nums`. For each number `num`, you'll generate new subsets by adding `num` to each of the existing subsets.

4. **Generating New Subsets**:
- `$ans_size = count($ans);`: Before modifying the `$ans` array, store its current size in `$ans_size`. This is necessary to prevent an infinite loop as new subsets are added.
- `for($i = 0; $i < $ans_size; $i++)`: Iterate over the current subsets stored in `$ans` up to `$ans_size`.
- `$cur = $ans[$i];`: Take the current subset.
- `$cur[] = $num;`: Append the current number `num` to this subset.
- `$ans[] = $cur;`: Add this new subset back into `$ans`.

5. **Return the Result**:
- `return $ans;`: After processing all numbers in `nums`, return the final array of all possible subsets.

### Example Execution:

Let's go through an example to see how it works.

- **Input**: `$nums = [1, 2]`
- **Initial State**: `$ans = [[]]`

**Iteration 1 (`num = 1`)**:
- `$ans_size = 1`: (Because there's only one subset, `[]`)
- For `i = 0`:
- `$cur = []`: Take the empty subset.
- `$cur[] = 1`: Append `1` to it, resulting in `[1]`.
- `$ans = [[], [1]]`: Add `[1]` to `$ans`.

**Iteration 2 (`num = 2`)**:
- `$ans_size = 2`: (Now there are two subsets: `[]` and `[1]`)
- For `i = 0`:
- `$cur = []`: Take the empty subset.
- `$cur[] = 2`: Append `2` to it, resulting in `[2]`.
- `$ans = [[], [1], [2]]`: Add `[2]` to `$ans`.
- For `i = 1`:
- `$cur = [1]`: Take the subset `[1]`.
- `$cur[] = 2`: Append `2` to it, resulting in `[1, 2]`.
- `$ans = [[], [1], [2], [1, 2]]`: Add `[1, 2]` to `$ans`.

**Final Result**:
- The function returns `[[], [1], [2], [1, 2]]`, which are all possible subsets of `[1, 2]`.

### Summary:

This function uses a dynamic approach to generate all subsets of an array by iteratively adding each element of the array to existing subsets. The time complexity is \(O(2^n)\), where \(n\) is the number of elements in the input array, because each element can either be included or excluded from each subset, resulting in \(2^n\) possible subsets.

**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)**
Loading