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

Merged
merged 10,000 commits into from
Jan 21, 2025
Merged

Develop #1201

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
1656410
Automated commit rollback 35
mah-shamim Jan 19, 2025
cb10d77
Automated commit 36
mah-shamim Jan 19, 2025
835f959
Automated commit rollback 36
mah-shamim Jan 19, 2025
881e3b3
Automated commit 37
mah-shamim Jan 19, 2025
4e957bc
Automated commit rollback 37
mah-shamim Jan 19, 2025
a4c6563
Automated commit 38
mah-shamim Jan 19, 2025
abbc7d5
Automated commit rollback 38
mah-shamim Jan 19, 2025
0d573b8
Automated commit 39
mah-shamim Jan 19, 2025
f09adf4
Automated commit rollback 39
mah-shamim Jan 19, 2025
af34ab5
Automated commit 40
mah-shamim Jan 19, 2025
18512b2
Automated commit rollback 40
mah-shamim Jan 19, 2025
b2fe29e
Automated commit 41
mah-shamim Jan 19, 2025
fcc4f9a
Automated commit rollback 41
mah-shamim Jan 19, 2025
92d60e1
Automated commit 42
mah-shamim Jan 19, 2025
351c0ca
Automated commit rollback 42
mah-shamim Jan 19, 2025
ba73ac5
Automated commit 43
mah-shamim Jan 19, 2025
42f26b3
Automated commit rollback 43
mah-shamim Jan 19, 2025
c9fe286
Automated commit 44
mah-shamim Jan 19, 2025
97edc73
Automated commit rollback 44
mah-shamim Jan 19, 2025
5c953a5
Automated commit 45
mah-shamim Jan 19, 2025
56f7995
Automated commit rollback 45
mah-shamim Jan 19, 2025
48c88a5
Automated commit 46
mah-shamim Jan 19, 2025
5751aeb
Automated commit rollback 46
mah-shamim Jan 19, 2025
00c64b6
Automated commit 47
mah-shamim Jan 19, 2025
2191c20
Automated commit rollback 47
mah-shamim Jan 19, 2025
21ee3ac
Automated commit 48
mah-shamim Jan 19, 2025
23ecfe0
Automated commit rollback 48
mah-shamim Jan 19, 2025
72ad563
Automated commit 49
mah-shamim Jan 19, 2025
4540fd8
Automated commit rollback 49
mah-shamim Jan 19, 2025
297dc03
Automated commit 50
mah-shamim Jan 19, 2025
21d8236
Automated commit rollback 50
mah-shamim Jan 19, 2025
040375c
#1179, #1180 leetcode problems 000407-trapping-rain-water-ii submissi…
mah-shamim Jan 19, 2025
7397fd3
Merge pull request #1181 from mah-shamim/000407-trapping-rain-water-ii
mah-shamim Jan 19, 2025
986cfaa
Merge pull request #1182 from mah-shamim/develop
mah-shamim Jan 19, 2025
055f943
#269, #270 leetcode problems 001382-balance-a-binary-search-tree subm…
mah-shamim Jan 19, 2025
e41cb5c
Merge pull request #1183 from mah-shamim/001382-balance-a-binary-sear…
mah-shamim Jan 19, 2025
517c44c
Merge pull request #1184 from mah-shamim/develop
mah-shamim Jan 19, 2025
bb0ab4d
Automated commit 1
mah-shamim Jan 20, 2025
ade040a
Automated commit rollback 1
mah-shamim Jan 20, 2025
80e901a
Automated commit 2
mah-shamim Jan 20, 2025
b277aa7
Automated commit rollback 2
mah-shamim Jan 20, 2025
e3a431a
Automated commit 3
mah-shamim Jan 20, 2025
9e01360
Automated commit rollback 3
mah-shamim Jan 20, 2025
47589bd
Automated commit 4
mah-shamim Jan 20, 2025
be15eca
Automated commit rollback 4
mah-shamim Jan 20, 2025
aa50293
Automated commit 5
mah-shamim Jan 20, 2025
2715d78
Automated commit rollback 5
mah-shamim Jan 20, 2025
37a67e6
Automated commit 6
mah-shamim Jan 20, 2025
c4ece9b
Automated commit rollback 6
mah-shamim Jan 20, 2025
3a0a097
Automated commit 7
mah-shamim Jan 20, 2025
6c6c886
Automated commit rollback 7
mah-shamim Jan 20, 2025
db66e0f
Automated commit 8
mah-shamim Jan 20, 2025
5847467
Automated commit rollback 8
mah-shamim Jan 20, 2025
41a46a5
Automated commit 9
mah-shamim Jan 20, 2025
0c264c0
Automated commit rollback 9
mah-shamim Jan 20, 2025
c13535c
Automated commit 10
mah-shamim Jan 20, 2025
7c0774c
Automated commit rollback 10
mah-shamim Jan 20, 2025
8f843f1
Automated commit 11
mah-shamim Jan 20, 2025
88781af
Automated commit rollback 11
mah-shamim Jan 20, 2025
202800f
Automated commit 12
mah-shamim Jan 20, 2025
054ca0f
Automated commit rollback 12
mah-shamim Jan 20, 2025
6ccb120
Automated commit 13
mah-shamim Jan 20, 2025
cf7f26c
Automated commit rollback 13
mah-shamim Jan 20, 2025
c1165c3
Automated commit 14
mah-shamim Jan 20, 2025
3906058
Automated commit rollback 14
mah-shamim Jan 20, 2025
df9675e
Automated commit 15
mah-shamim Jan 20, 2025
ad8a00a
Automated commit rollback 15
mah-shamim Jan 20, 2025
ea2bf4d
Automated commit 16
mah-shamim Jan 20, 2025
96bee8d
Automated commit rollback 16
mah-shamim Jan 20, 2025
4f54a30
Automated commit 17
mah-shamim Jan 20, 2025
1860c3c
Automated commit rollback 17
mah-shamim Jan 20, 2025
265834c
Automated commit 18
mah-shamim Jan 20, 2025
539afdd
Automated commit rollback 18
mah-shamim Jan 20, 2025
fb58a2d
Automated commit 19
mah-shamim Jan 20, 2025
3a2b4d6
Automated commit rollback 19
mah-shamim Jan 20, 2025
633ad01
Automated commit 20
mah-shamim Jan 20, 2025
67b4684
Automated commit rollback 20
mah-shamim Jan 20, 2025
0b892a4
Automated commit 21
mah-shamim Jan 20, 2025
42c5d79
Automated commit rollback 21
mah-shamim Jan 20, 2025
162e84b
Automated commit 22
mah-shamim Jan 20, 2025
c471405
Automated commit rollback 22
mah-shamim Jan 20, 2025
3227556
Automated commit 23
mah-shamim Jan 20, 2025
5d2f29d
Automated commit rollback 23
mah-shamim Jan 20, 2025
1db3932
Automated commit 24
mah-shamim Jan 20, 2025
dec3ff7
Automated commit rollback 24
mah-shamim Jan 20, 2025
af85b30
Automated commit 25
mah-shamim Jan 20, 2025
818f38b
Automated commit rollback 25
mah-shamim Jan 20, 2025
d2d0005
Automated commit 26
mah-shamim Jan 20, 2025
c4dd4f1
Automated commit rollback 26
mah-shamim Jan 20, 2025
be94d21
Automated commit 27
mah-shamim Jan 20, 2025
484698c
Automated commit rollback 27
mah-shamim Jan 20, 2025
8fbd587
Automated commit 28
mah-shamim Jan 20, 2025
9caba9d
Automated commit rollback 28
mah-shamim Jan 20, 2025
7518344
Automated commit 29
mah-shamim Jan 20, 2025
8c5d99d
Automated commit rollback 29
mah-shamim Jan 20, 2025
c252233
Automated commit 30
mah-shamim Jan 20, 2025
f1f7a5d
Automated commit rollback 30
mah-shamim Jan 20, 2025
5f39103
Automated commit 31
mah-shamim Jan 20, 2025
1ecd6e1
Automated commit rollback 31
mah-shamim Jan 20, 2025
a6d6f7f
Automated commit 32
mah-shamim Jan 20, 2025
ceda638
Automated commit rollback 32
mah-shamim Jan 20, 2025
a8b2277
Automated commit 33
mah-shamim Jan 20, 2025
ba1a53d
Automated commit rollback 33
mah-shamim Jan 20, 2025
454d280
Automated commit 34
mah-shamim Jan 20, 2025
0538e3b
Automated commit rollback 34
mah-shamim Jan 20, 2025
94f9c12
Automated commit 35
mah-shamim Jan 20, 2025
5291204
Automated commit rollback 35
mah-shamim Jan 20, 2025
02e8ac4
Automated commit 36
mah-shamim Jan 20, 2025
5760a04
Automated commit rollback 36
mah-shamim Jan 20, 2025
906c90c
Automated commit 37
mah-shamim Jan 20, 2025
0154b22
Automated commit rollback 37
mah-shamim Jan 20, 2025
4c5a3ba
Automated commit 38
mah-shamim Jan 20, 2025
11d4329
Automated commit rollback 38
mah-shamim Jan 20, 2025
d346ba6
Automated commit 39
mah-shamim Jan 20, 2025
3ca9b32
Automated commit rollback 39
mah-shamim Jan 20, 2025
12634f2
Automated commit 40
mah-shamim Jan 20, 2025
f9d7552
Automated commit rollback 40
mah-shamim Jan 20, 2025
e9f5f64
Automated commit 41
mah-shamim Jan 20, 2025
1ce71d6
Automated commit rollback 41
mah-shamim Jan 20, 2025
7382971
Automated commit 42
mah-shamim Jan 20, 2025
f1362ae
Automated commit rollback 42
mah-shamim Jan 20, 2025
c3bcf4b
Automated commit 43
mah-shamim Jan 20, 2025
560a388
Automated commit rollback 43
mah-shamim Jan 20, 2025
22c710c
Automated commit 44
mah-shamim Jan 20, 2025
d6f0c58
Automated commit rollback 44
mah-shamim Jan 20, 2025
1e19181
Automated commit 45
mah-shamim Jan 20, 2025
df7e563
Automated commit rollback 45
mah-shamim Jan 20, 2025
f6d8681
Automated commit 46
mah-shamim Jan 20, 2025
8306823
Automated commit rollback 46
mah-shamim Jan 20, 2025
edf34d5
Automated commit 47
mah-shamim Jan 20, 2025
1ba0bf7
Automated commit rollback 47
mah-shamim Jan 20, 2025
609d8dc
Automated commit 48
mah-shamim Jan 20, 2025
22c00ff
Automated commit rollback 48
mah-shamim Jan 20, 2025
5078b7b
Automated commit 49
mah-shamim Jan 20, 2025
b1c4f10
Automated commit rollback 49
mah-shamim Jan 20, 2025
262254a
Automated commit 50
mah-shamim Jan 20, 2025
1210b04
Automated commit rollback 50
mah-shamim Jan 20, 2025
cbe8782
#1185, #1186 leetcode problems 002661-first-completely-painted-row-or…
mah-shamim Jan 20, 2025
67d65f2
Merge pull request #1187 from mah-shamim/002661-first-completely-pain…
mah-shamim Jan 20, 2025
362b096
Merge pull request #1188 from mah-shamim/develop
mah-shamim Jan 20, 2025
c5c330b
#1185, #1186 leetcode problems 002661-first-completely-painted-row-or…
mah-shamim Jan 20, 2025
506b3fe
#1185, #1186 leetcode problems 002661-first-completely-painted-row-or…
mah-shamim Jan 20, 2025
658f1a5
#271, #272 leetcode problems 001404-number-of-steps-to-reduce-a-numbe…
mah-shamim Jan 20, 2025
ba02067
Merge pull request #1189 from mah-shamim/001404-number-of-steps-to-re…
mah-shamim Jan 20, 2025
899650d
Merge pull request #1190 from mah-shamim/develop
mah-shamim Jan 20, 2025
31dcbf1
Automated commit 1
mah-shamim Jan 21, 2025
0c8111a
Automated commit rollback 1
mah-shamim Jan 21, 2025
9c2853a
Automated commit 2
mah-shamim Jan 21, 2025
84141f7
Automated commit rollback 2
mah-shamim Jan 21, 2025
2fcad48
Automated commit 3
mah-shamim Jan 21, 2025
1f0b775
Automated commit rollback 3
mah-shamim Jan 21, 2025
582994b
Automated commit 4
mah-shamim Jan 21, 2025
a95e219
Automated commit rollback 4
mah-shamim Jan 21, 2025
031e174
Automated commit 5
mah-shamim Jan 21, 2025
24af61f
Automated commit rollback 5
mah-shamim Jan 21, 2025
ddbd112
Automated commit 6
mah-shamim Jan 21, 2025
90c0f24
Automated commit rollback 6
mah-shamim Jan 21, 2025
ac55ca4
Automated commit 7
mah-shamim Jan 21, 2025
1df5f20
Automated commit rollback 7
mah-shamim Jan 21, 2025
57029e5
Automated commit 8
mah-shamim Jan 21, 2025
a4a2569
Automated commit rollback 8
mah-shamim Jan 21, 2025
14199c6
Automated commit 9
mah-shamim Jan 21, 2025
18f9489
Automated commit rollback 9
mah-shamim Jan 21, 2025
c42c750
Automated commit 10
mah-shamim Jan 21, 2025
e9ee37f
Automated commit rollback 10
mah-shamim Jan 21, 2025
cd8fde8
Automated commit 11
mah-shamim Jan 21, 2025
c5addf0
Automated commit rollback 11
mah-shamim Jan 21, 2025
4d155c4
Automated commit 12
mah-shamim Jan 21, 2025
2b454d6
Automated commit rollback 12
mah-shamim Jan 21, 2025
173e42d
Automated commit 13
mah-shamim Jan 21, 2025
39227d8
Automated commit rollback 13
mah-shamim Jan 21, 2025
dc47cae
Automated commit 14
mah-shamim Jan 21, 2025
841ebf4
Automated commit rollback 14
mah-shamim Jan 21, 2025
151d7d2
Automated commit 15
mah-shamim Jan 21, 2025
fdb88a8
Automated commit rollback 15
mah-shamim Jan 21, 2025
bf62946
Automated commit 16
mah-shamim Jan 21, 2025
b4a91a1
Automated commit rollback 16
mah-shamim Jan 21, 2025
cba251f
Automated commit 17
mah-shamim Jan 21, 2025
252f027
Automated commit rollback 17
mah-shamim Jan 21, 2025
9ce0223
Automated commit 18
mah-shamim Jan 21, 2025
fb25031
Automated commit rollback 18
mah-shamim Jan 21, 2025
be51283
Automated commit 19
mah-shamim Jan 21, 2025
b595117
Automated commit rollback 19
mah-shamim Jan 21, 2025
a13b621
Automated commit 20
mah-shamim Jan 21, 2025
4978d2d
Automated commit rollback 20
mah-shamim Jan 21, 2025
0b46aa2
Automated commit 21
mah-shamim Jan 21, 2025
c34b3c9
Automated commit rollback 21
mah-shamim Jan 21, 2025
de13a73
Automated commit 22
mah-shamim Jan 21, 2025
38d164a
Automated commit rollback 22
mah-shamim Jan 21, 2025
18bf087
Automated commit 23
mah-shamim Jan 21, 2025
47c87e9
Automated commit rollback 23
mah-shamim Jan 21, 2025
1aa403c
Automated commit 24
mah-shamim Jan 21, 2025
c815905
Automated commit rollback 24
mah-shamim Jan 21, 2025
f5c35e6
Automated commit 25
mah-shamim Jan 21, 2025
6ec7bd6
Automated commit rollback 25
mah-shamim Jan 21, 2025
804ec44
Automated commit 26
mah-shamim Jan 21, 2025
be2f726
Automated commit rollback 26
mah-shamim Jan 21, 2025
f80a5bd
Automated commit 27
mah-shamim Jan 21, 2025
969f7b4
Automated commit rollback 27
mah-shamim Jan 21, 2025
c6c91eb
Automated commit 28
mah-shamim Jan 21, 2025
215b81a
Automated commit rollback 28
mah-shamim Jan 21, 2025
0589d75
Automated commit 29
mah-shamim Jan 21, 2025
ee6d028
Automated commit rollback 29
mah-shamim Jan 21, 2025
64ef409
Automated commit 30
mah-shamim Jan 21, 2025
7da31ec
Automated commit rollback 30
mah-shamim Jan 21, 2025
d9cf8f2
Automated commit 31
mah-shamim Jan 21, 2025
0d843ed
Automated commit rollback 31
mah-shamim Jan 21, 2025
c8cda39
Automated commit 32
mah-shamim Jan 21, 2025
3fc7041
Automated commit rollback 32
mah-shamim Jan 21, 2025
16cb647
Automated commit 33
mah-shamim Jan 21, 2025
3e495f6
Automated commit rollback 33
mah-shamim Jan 21, 2025
6a01778
Automated commit 34
mah-shamim Jan 21, 2025
bb1e9e1
Automated commit rollback 34
mah-shamim Jan 21, 2025
8329300
Automated commit 35
mah-shamim Jan 21, 2025
91d3bcf
Automated commit rollback 35
mah-shamim Jan 21, 2025
9209b24
Automated commit 36
mah-shamim Jan 21, 2025
3d1d9b4
Automated commit rollback 36
mah-shamim Jan 21, 2025
ebbbe02
Automated commit 37
mah-shamim Jan 21, 2025
41401d0
Automated commit rollback 37
mah-shamim Jan 21, 2025
57e2c09
Automated commit 38
mah-shamim Jan 21, 2025
fe38b76
Automated commit rollback 38
mah-shamim Jan 21, 2025
2328768
Automated commit 39
mah-shamim Jan 21, 2025
db0783b
Automated commit rollback 39
mah-shamim Jan 21, 2025
12506a8
Automated commit 40
mah-shamim Jan 21, 2025
d694567
Automated commit rollback 40
mah-shamim Jan 21, 2025
c90c30a
Automated commit 41
mah-shamim Jan 21, 2025
90d2e98
Automated commit rollback 41
mah-shamim Jan 21, 2025
96038e0
Automated commit 42
mah-shamim Jan 21, 2025
78a0586
Automated commit rollback 42
mah-shamim Jan 21, 2025
4c57d2d
Automated commit 43
mah-shamim Jan 21, 2025
a3d2cac
Automated commit rollback 43
mah-shamim Jan 21, 2025
84166f2
Automated commit 44
mah-shamim Jan 21, 2025
06a28ff
Automated commit rollback 44
mah-shamim Jan 21, 2025
98383cb
Automated commit 45
mah-shamim Jan 21, 2025
2316799
Automated commit rollback 45
mah-shamim Jan 21, 2025
bd13a00
Automated commit 46
mah-shamim Jan 21, 2025
8c9cf96
Automated commit rollback 46
mah-shamim Jan 21, 2025
93d127f
Automated commit 47
mah-shamim Jan 21, 2025
93e1952
Automated commit rollback 47
mah-shamim Jan 21, 2025
5a046a5
Automated commit 48
mah-shamim Jan 21, 2025
18b57b5
Automated commit rollback 48
mah-shamim Jan 21, 2025
ebb0ba0
Automated commit 49
mah-shamim Jan 21, 2025
fe0cb58
Automated commit rollback 49
mah-shamim Jan 21, 2025
2847d25
Automated commit 50
mah-shamim Jan 21, 2025
8de749c
Automated commit rollback 50
mah-shamim Jan 21, 2025
7d35d2f
#1191, #1192 leetcode problems 002017-grid-game submissions 1515964773
mah-shamim Jan 21, 2025
062553a
Merge pull request #1193 from mah-shamim/002017-grid-game
mah-shamim Jan 21, 2025
61b49e7
Merge pull request #1194 from mah-shamim/develop
mah-shamim Jan 21, 2025
17f050b
#273, #274 leetcode problems 001438-longest-continuous-subarray-with-…
mah-shamim Jan 21, 2025
269f30d
Merge pull request #1195 from mah-shamim/001438-longest-continuous-su…
mah-shamim Jan 21, 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