Skip to content

Commit 629a536

Browse files
authored
Merge pull request #145 from BrianLusina/feat/algorithms-two-pointers
feat(algorithms): two pointers
2 parents 8c30b66 + 6df7b66 commit 629a536

File tree

135 files changed

+615
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+615
-388
lines changed

DIRECTORY.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
* [Test Sorted Squared Array](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/sorted_squared_array/test_sorted_squared_array.py)
2020
* Subsequence
2121
* [Test Is Valid Subsequence](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/subsequence/test_is_valid_subsequence.py)
22-
* Two Sum
23-
* [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/two_sum/test_two_sum.py)
24-
* Two Sum Less K
25-
* [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/two_sum_less_k/test_two_sum.py)
2622
* Backtracking
2723
* Combination
2824
* [Test Combination 2](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/backtracking/combination/test_combination_2.py)
@@ -210,20 +206,32 @@
210206
* Taxi Numbers
211207
* [Taxi Numbers](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/taxi_numbers/taxi_numbers.py)
212208
* Two Pointers
209+
* Array 3 Pointers
210+
* [Test Array 3 Pointers](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/array_3_pointers/test_array_3_pointers.py)
213211
* Find Sum Of Three
214212
* [Test Find Sum Of Three](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/find_sum_of_three/test_find_sum_of_three.py)
215213
* Merge Sorted Arrays
216214
* [Test Merge Sorted Arrays](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/merge_sorted_arrays/test_merge_sorted_arrays.py)
215+
* Move Zeroes
216+
* [Test Move Zeroes](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/move_zeroes/test_move_zeroes.py)
217217
* Next Permutation
218218
* [Test Next Permutation](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/next_permutation/test_next_permutation.py)
219219
* Pair With Sum In Array
220220
* [Test Pair With Sum In Array](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/pair_with_sum_in_array/test_pair_with_sum_in_array.py)
221+
* Rain Water Trapped
222+
* [Test Trapped Rain Water](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/rain_water_trapped/test_trapped_rain_water.py)
221223
* Reverse Array
222224
* [Test Reverse Array](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/reverse_array/test_reverse_array.py)
223225
* Sort Colors
224226
* [Test Sort Colors](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/sort_colors/test_sort_colors.py)
225227
* Three Sum
226228
* [Test Three Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/three_sum/test_three_sum.py)
229+
* Triangle Numbers
230+
* [Test Triangle Numbers](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/triangle_numbers/test_triangle_numbers.py)
231+
* Two Sum
232+
* [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/two_sum/test_two_sum.py)
233+
* Two Sum Less K
234+
* [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/two_pointers/two_sum_less_k/test_two_sum.py)
227235
* Unique Bsts
228236
* [Unique Bsts](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/unique_bsts/unique_bsts.py)
229237
* Word Count
@@ -577,8 +585,6 @@
577585
* Allergies
578586
* [Test Allergies](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/allergies/test_allergies.py)
579587
* Arrays
580-
* Array 3 Pointers
581-
* [Test Array 3 Pointers](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/array_3_pointers/test_array_3_pointers.py)
582588
* Can Place Flowers
583589
* [Test Can Place Flowers](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/can_place_flowers/test_can_place_flowers.py)
584590
* Candy
@@ -605,12 +611,8 @@
605611
* [Test Max Average Subarray](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/maximum_average_subarray/test_max_average_subarray.py)
606612
* Maxlen Contiguous Binary Subarray
607613
* [Test Maxlen Contiguous Binary Subarray](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/maxlen_contiguous_binary_subarray/test_maxlen_contiguous_binary_subarray.py)
608-
* Move Zeroes
609-
* [Test Move Zeroes](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/move_zeroes/test_move_zeroes.py)
610614
* Product Of Array Except Self
611615
* [Test Product Except Self](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/product_of_array_except_self/test_product_except_self.py)
612-
* Rain Water Trapped
613-
* [Test Trapped Rain Water](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/rain_water_trapped/test_trapped_rain_water.py)
614616
* Rotation
615617
* Cyclic Rotation
616618
* [Test Cyclic Rotation](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/rotation/cyclic_rotation/test_cyclic_rotation.py)

algorithms/intervals/insert_interval/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ Return the updated list of intervals.
2525

2626
## Solution
2727

28-
We first want to create a new list merged to store the merged intervals we will return at the end.
28+
We first want to create a new list `merged` to store the merged intervals we will return at the end.
2929

3030
This solution operates in 3 phases:
31-
1. Add all the intervals ending before newInterval starts to merged.
32-
2. Merge all overlapping intervals with newInterval and add that merged interval to merged.
33-
3. Add all the intervals starting after newInterval to merged.
31+
1. Add all the intervals ending before `newInterval` starts to `merged`.
32+
2. Merge all overlapping intervals with `newInterval` and add that merged interval to `merged`.
33+
3. Add all the intervals starting after `newInterval` to `merged`.
3434

3535
### Phase 1
3636

37-
In this phase, we add all the intervals that end before newInterval starts to merged. This involves iterating through the
38-
intervals list until the current interval no longer ends before newInterval starts (i.e. intervals[i][1] >= newInterval[0]).
37+
In this phase, we add all the intervals that end before `newInterval` starts to `merged`. This involves iterating through the
38+
`intervals` list until the current interval no longer ends before `newInterval` starts (i.e. `intervals[i][1] >= newInterval[0]`).
3939

4040
![Solution 1](./images/solutions/insert_interval_solution_1.png)
4141
![Solution 2](./images/solutions/insert_interval_solution_2.png)
4242

4343
### Phase 2
4444

4545
In this phase, we merge all the intervals that overlap with newInterval together into a single interval by updating
46-
newInterval to be the minimum start and maximum end of all the overlapping intervals. This involves iterating through
47-
the intervals list until the current interval starts after newInterval ends (i.e. intervals[i][0] > newInterval[1]).
48-
When that condition is met, we add newInterval to merged and move onto phase 3.
46+
`newInterval` to be the minimum start and maximum end of all the overlapping intervals. This involves iterating through
47+
the intervals list until the current interval starts after `newInterval` ends (i.e. `intervals[i][0] > newInterval[1]`).
48+
When that condition is met, we add `newInterval` to merged and move onto phase 3.
4949

5050
![Solution 3](./images/solutions/insert_interval_solution_3.png)
5151
![Solution 4](./images/solutions/insert_interval_solution_4.png)
File renamed without changes.
File renamed without changes.

puzzles/arrays/array_3_pointers/test_array_3_pointers.py renamed to algorithms/two_pointers/array_3_pointers/test_array_3_pointers.py

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Move Zeroes
2+
3+
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero
4+
elements.
5+
6+
Note that you must do this in-place without making a copy of the array.
7+
8+
```plain
9+
10+
Example 1:
11+
12+
Input: nums = [0,1,0,3,12]
13+
Output: [1,3,12,0,0]
14+
Example 2:
15+
16+
Input: nums = [0]
17+
Output: [0]
18+
```
19+
20+
## Related Topics
21+
22+
- Array
23+
- Two Pointers
24+
25+
## Solution
26+
27+
We can solve this problem by keeping a pointer i that iterates through the array and another pointer nextNonZero that
28+
points to the position where the next non-zero element should be placed. We can then swap the elements at i and nextNonZero
29+
if the element at i is non-zero. This way, we can maintain the relative order of the non-zero elements while moving all
30+
the zeroes to the end of the array.
31+
32+
![Solution 1](./images/solutions/move_zeroes_solution_1.png)
33+
![Solution 2](./images/solutions/move_zeroes_solution_2.png)
34+
![Solution 3](./images/solutions/move_zeroes_solution_3.png)
35+
![Solution 4](./images/solutions/move_zeroes_solution_4.png)
36+
![Solution 5](./images/solutions/move_zeroes_solution_5.png)
37+
![Solution 6](./images/solutions/move_zeroes_solution_6.png)
38+
![Solution 7](./images/solutions/move_zeroes_solution_7.png)
39+
![Solution 8](./images/solutions/move_zeroes_solution_8.png)
40+
![Solution 9](./images/solutions/move_zeroes_solution_9.png)
41+
![Solution 10](./images/solutions/move_zeroes_solution_10.png)
42+
![Solution 11](./images/solutions/move_zeroes_solution_11.png)
43+
![Solution 12](./images/solutions/move_zeroes_solution_12.png)
44+
![Solution 13](./images/solutions/move_zeroes_solution_13.png)

puzzles/arrays/move_zeroes/__init__.py renamed to algorithms/two_pointers/move_zeroes/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ def move_zeroes(nums: List[int]) -> None:
1919
if len(nums) == 1:
2020
return
2121

22-
left_pointer = 0
23-
for current in range(len(nums)):
24-
if nums[current] != 0:
25-
nums[left_pointer], nums[current] = nums[current], nums[left_pointer]
26-
left_pointer += 1
22+
next_non_zero = 0
23+
for idx in range(len(nums)):
24+
if nums[idx] != 0:
25+
if idx != next_non_zero:
26+
nums[next_non_zero], nums[idx] = nums[idx], nums[next_non_zero]
27+
next_non_zero += 1
2728

2829

2930
def move_zeroes_one(nums: List[int]) -> None:
22.6 KB
Loading
26.2 KB
Loading
29.3 KB
Loading

0 commit comments

Comments
 (0)