-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms): two pointers #145
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c95ac53
docs(algorithms, two-pointers): three sum documentation
BrianLusina 6b00d0e
feat(algorithms, two-pointers): triangle numbers
BrianLusina b5bf32f
updating DIRECTORY.md
8ccae81
refactor(algorithms, two-pointers, move-zeroes): move algorithm to co…
BrianLusina 5c8e273
updating DIRECTORY.md
b1d3baf
chore(docstring): fix typo
BrianLusina 6aada08
refactor(algorithms, two-pointers, sort-colors): add documentation an…
BrianLusina cf07d94
refactor(algorithms, two-pointers): trapped rain water
BrianLusina 3efcce4
updating DIRECTORY.md
e899c0b
test(algorithms, two-pointers, move_zeroes): update image reference
BrianLusina 6df7b66
test(algorithms, two-pointers, move_zeroes): update type hint in test
BrianLusina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+38.2 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.1 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.3 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.4 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+39.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+32.5 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.7 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+32.3 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+35.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+35.8 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.6 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+36.9 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.2 KB
algorithms/two_pointers/three_sum/images/solutions/three_sum_solution_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Triangle Numbers | ||
|
|
||
| Write a function to count the number of triplets in an integer array nums that could form the sides of a triangle. For | ||
| three sides to form a valid triangle, the sum of any two sides must be greater than the third side. The triplets do not | ||
| need to be unique. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```text | ||
| Input: | ||
| nums = [11,4,9,6,15,18] | ||
|
|
||
| Output: | ||
| 10 | ||
|
|
||
| Explanation: Valid combinations are... | ||
|
|
||
| 4, 15, 18 | ||
| 6, 15, 18 | ||
| 9, 15, 18 | ||
| 11, 15, 18 | ||
| 9, 11, 18 | ||
| 6, 11, 15 | ||
| 9, 11, 15 | ||
| 4, 6, 9 | ||
| ``` | ||
|
|
||
| ## Solution | ||
|
|
||
| In order for a triplet to be valid lengths of a triangle, the sum of any two sides must be greater than the third side. | ||
| By sorting the array, we can leverage the two-pointer technique to count all valid triplets in O(n2) time and O(1) space. | ||
| The key to this question is realizing that if we sort three numbers from smallest to largest (say a ≤ b ≤ c), we only | ||
| need to check if a + b > c. If this condition holds, the other two conditions (a + c > b and b + c > a) are automatically | ||
| satisfied because c ≥ b and b ≥ a. For example, with 4, 8, 9, if 4 + 8 > 9 is true, then we have a valid triplet. | ||
|
|
||
|  | ||
|
|
||
| But not only that, triplets where the smallest number is between 4 and 8 are also valid triplets. | ||
|
|
||
|  | ||
|
|
||
| This means that if we sort the input array, and then iterate from the end of the array to the beginning, we can use the | ||
| two-pointer technique to efficiently count all valid triplets. | ||
|
|
||
|  | ||
|
|
||
| The pointers i, left, and right represent the current triplet we are considering. If nums[left] + nums[right] > nums[i] | ||
| then we know there are a total of right - left valid triplets, since all triplets between left and right are also valid | ||
| triplets. We can then decrement right to check for the valid triplets that can be made by decreasing the middle value. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| When nums[left] + nums[right] < nums[i], we know that all triplets between left and right are also invalid, so we | ||
| increment left to look for a larger smallest value. | ||
|
|
||
|  | ||
|
|
||
| Each time left and right cross, we decrement i and reset left and right to their positions at opposite ends of the array. | ||
| This happens until i is less than 2, at which point we have counted all valid triplets. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| def triangle_number(heights: List[int]) -> int: | ||
| """ | ||
| Finds the count of valid triangles that can be formed from the given input of numbers. A valid triangle is a triangle | ||
| which has any two sides whose sum is greater than the third side. This assumes that it is okay to manipulate the | ||
| input list. Therefore callers of this function should be aware that the input list is manipulated in place. | ||
|
|
||
| Args: | ||
| heights (list): list of integers that represent sides of a triangle | ||
| Returns: | ||
| int: number of valid triangles that can be formed | ||
| """ | ||
| # If there are no heights, or we have an empty list, return 0 early as no valid triangles can be formed here. | ||
| if not heights: | ||
| return 0 | ||
|
|
||
| # Sorts the heights in place. This incurs a time complexity cost of O(n log(n)) and space cost of O(n) as this sorting | ||
| # requires temporary storage using Python's timsort | ||
| heights.sort() | ||
|
|
||
| # Keeps track of number of valid triangles that can be formed | ||
| count = 0 | ||
|
|
||
| # Iterate through the list starting from the back, idx will be at the last position, this will be the third pointer | ||
| for idx in range(len(heights) - 1, 1, -1): | ||
| # Initialize the two pointers to keep track of the other two indices that will point to the two other numbers that | ||
| # can form a valid triangle. | ||
| left = 0 | ||
| right = idx - 1 | ||
|
|
||
| # This is a micro-optimization to get the largest side and use it in the loop below | ||
| largest_side = heights[idx] | ||
|
|
||
| while left < right: | ||
| # A valid triplet is found by satisfying the condition a + b > c. If this condition holds, then the other | ||
| # two conditions hold as well, a + c > b and b + c > a. | ||
| is_valid_triplet = heights[left] + heights[right] > largest_side | ||
| if is_valid_triplet: | ||
| # The numbers between the right and left pointers form valid triplets with the number at the idx position | ||
| # we find all the possible triplets(triangles) that can be formed by finding the difference. | ||
| count += right - left | ||
| # we decrement right to check if there are valid triplets that can be formed by decreasing the middle valid | ||
| right -= 1 | ||
| else: | ||
| # Increase the left to find the next maximum minimum number that can form a valid triplet | ||
| left += 1 | ||
|
|
||
| # return the count of the triangles that can be formed | ||
| return count |
Binary file added
BIN
+15.7 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.2 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.8 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.3 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.9 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.8 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.1 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_15.png
Oops, something went wrong.
Binary file added
BIN
+31 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_16.png
Oops, something went wrong.
Binary file added
BIN
+30.4 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_17.png
Oops, something went wrong.
Binary file added
BIN
+25.6 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_18.png
Oops, something went wrong.
Binary file added
BIN
+31.6 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_19.png
Oops, something went wrong.
Binary file added
BIN
+40.9 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_2.png
Oops, something went wrong.
Binary file added
BIN
+37.6 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_20.png
Oops, something went wrong.
Binary file added
BIN
+31.9 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_21.png
Oops, something went wrong.
Binary file added
BIN
+30.7 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_22.png
Oops, something went wrong.
Binary file added
BIN
+26.8 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_23.png
Oops, something went wrong.
Binary file added
BIN
+30.8 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_24.png
Oops, something went wrong.
Binary file added
BIN
+34.8 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_25.png
Oops, something went wrong.
Binary file added
BIN
+32.5 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_26.png
Oops, something went wrong.
Binary file added
BIN
+27.6 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_27.png
Oops, something went wrong.
Binary file added
BIN
+26.2 KB
...two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_28.png
Oops, something went wrong.
Binary file added
BIN
+32.8 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_3.png
Oops, something went wrong.
Binary file added
BIN
+23.9 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_4.png
Oops, something went wrong.
Binary file added
BIN
+31.5 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_5.png
Oops, something went wrong.
Binary file added
BIN
+42 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_6.png
Oops, something went wrong.
Binary file added
BIN
+31.3 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_7.png
Oops, something went wrong.
Binary file added
BIN
+30.9 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_8.png
Oops, something went wrong.
Binary file added
BIN
+30.3 KB
.../two_pointers/triangle_numbers/images/solutions/triangle_numbers_solution_9.png
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
algorithms/two_pointers/triangle_numbers/test_triangle_numbers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from algorithms.two_pointers.triangle_numbers import triangle_number | ||
|
|
||
| TRIANGLE_NUMBER_TEST_CASES = [ | ||
| ([11, 4, 9, 6, 15, 18], 10), | ||
| ([2,2,3,4], 3), | ||
| ([4,2,3,4], 4), | ||
| ] | ||
|
|
||
|
|
||
| class TriangleNumberTestCases(unittest.TestCase): | ||
| @parameterized.expand(TRIANGLE_NUMBER_TEST_CASES) | ||
| def test_triangle_number(self, heights: List[int], expected: int): | ||
| actual = triangle_number(heights) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.