-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms): sliding window #155
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 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3cab43b
feat(algorithms, sliding-window, max sum subarray): max sum subarray
BrianLusina 4f77531
updating DIRECTORY.md
eff6568
feat(algorithms, sliding-window): max points from cards
BrianLusina 23a91a1
updating DIRECTORY.md
dbcbcb5
chore(algorithms, sliding-window, max sum-subarray): account for all …
BrianLusina fae454f
feat(data-structures, binary-tree): mirror binary tree
BrianLusina b88d25b
updating DIRECTORY.md
1e5bdc7
Update datastructures/trees/binary/tree/tree_utils.py
BrianLusina 635dff6
feat(algorithms, greedy): assign cookies
BrianLusina b71f628
docs(algorithms, greedy): gas stations
BrianLusina d374048
docs(algorithms, greedy): jump game
BrianLusina 510b0ff
updating DIRECTORY.md
80c3de0
feat(algorithms, greedy): two city scheduling
BrianLusina 22ffbd4
feat(algorithms, greedy): min number of refueling stops
BrianLusina f95eb2d
updating DIRECTORY.md
40b083a
refactor(algorithms, fast-and-slow): find duplicate number
BrianLusina e0bcd63
Update algorithms/dynamic_programming/buy_sell_stock/__init__.py
BrianLusina e19a1fc
Update algorithms/greedy/assign_cookies/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,5 +126,3 @@ Output: 6 | |
| - Arrays | ||
| - Dynamic Programming | ||
| - Greedy | ||
|
|
||
|
|
||
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
5 changes: 4 additions & 1 deletion
5
algorithms/dynamic_programming/buy_sell_stock/test_max_profit.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
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,84 @@ | ||
| # Assign Cookies | ||
|
|
||
| Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most | ||
|
BrianLusina marked this conversation as resolved.
|
||
| one cookie. | ||
|
|
||
| Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and | ||
| each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. | ||
| Your goal is to maximize the number of your content children and output the maximum number. | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 1 <= greed.length <= 3 * 104 | ||
| - 0 <= cookies.length <= 3 * 104 | ||
| - 1 <= greed[i], cookies[j] <= 231 - 1 | ||
|
BrianLusina marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Examples | ||
|
|
||
| Example 1: | ||
| ```text | ||
| Input: g = [1,2,3], s = [1,1] | ||
| Output: 1 | ||
| Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. | ||
| And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 | ||
| content. | ||
| You need to output 1. | ||
| ``` | ||
|
|
||
| Example 2: | ||
| ```text | ||
| Input: g = [1,2], s = [1,2,3] | ||
| Output: 2 | ||
| Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. | ||
| You have 3 cookies and their sizes are big enough to gratify all of the children, | ||
| You need to output 2. | ||
| ``` | ||
|
|
||
| ## Topics | ||
|
|
||
| - Array | ||
| - Two Pointers | ||
| - Greedy | ||
| - Sorting | ||
|
|
||
| ## Solution | ||
|
|
||
| Intiutively, we want to give each child the smallest cookie that satisfies them. This allows us to save the larger | ||
| cookies for the greedier children and allows us to maximize the number of satisfied children. | ||
|
|
||
| The greedy algorithm sorts both the greeds and cookies arrays in ascending order. This places the child with the smallest | ||
| greed and the smallest cookie at the front of each array. | ||
|
|
||
| For example: | ||
|
|
||
| ```text | ||
| greeds = [1, 3, 3, 4] | ||
| cookies = [2, 2, 3, 4] | ||
| ``` | ||
|
|
||
| We then initialize two pointers `i` and `j` to the start of the `greeds` and `cookies` arrays, respectively. `i` | ||
| represents the current child and `j` represents the current cookie. | ||
|
|
||
| If `cookies[j] >= greeds[i]`, that means the current cookie can satisfy the current child. We increment the number of | ||
| satisfied children and move to the next child and cookie. | ||
|
|
||
|  | ||
|
|
||
| If `cookies[j] < greeds[i]`, that means the current cookie cannot satisfy the current child, so we move to the next cookie | ||
| to see if it can. | ||
|
|
||
|  | ||
|
|
||
| We can continue this process until we reach the end of either the greeds or cookies arrays, and return the number of | ||
| satisfied children as the result. | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| #### Time Complexity | ||
|
|
||
| O(n log n + m log m) where n is the number of children and m is the number of cookies. We sort the | ||
| greeds and cookies arrays in O(n log n + m log m) time, and then iterate through the arrays in O(n + m) time. | ||
|
|
||
| #### Space Complexity | ||
|
|
||
| O(1) We only use a constant amount of space for variables. | ||
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 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| def find_content_children(greeds: List[int], cookies: List[int]) -> int: | ||
| # This in-place sorting of both g and s results in a time complexity of O(n log(n) + m log(m)) | ||
| greeds.sort() | ||
| cookies.sort() | ||
|
|
||
| cookie, greed = 0, 0 | ||
| count = 0 | ||
|
|
||
| # We iterate through each greed factor and cookie | ||
| while greed < len(greeds) and cookie < len(cookies): | ||
| # When we get a cookie that satisfies kid i, we assign that cookie to the child | ||
| # and move along, increasing the count as well | ||
| if cookies[cookie] >= greeds[greed]: | ||
| count += 1 | ||
| greed += 1 | ||
| cookie += 1 | ||
|
|
||
| return count |
Binary file added
BIN
+83 KB
algorithms/greedy/assign_cookies/images/solutions/assign_cookies_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
+45.2 KB
algorithms/greedy/assign_cookies/images/solutions/assign_cookies_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.
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,24 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from algorithms.greedy.assign_cookies import find_content_children | ||
|
|
||
|
|
||
| ASSIGN_COOKIES_TEST_CASES = [ | ||
| ([1, 2, 3], [1, 1], 1), | ||
| ([1, 2], [1, 2, 3], 2), | ||
| ([10, 9, 8, 7], [5, 6, 7, 8], 2), | ||
| ] | ||
|
|
||
|
|
||
| class AssignCookiesTestCase(unittest.TestCase): | ||
| @parameterized.expand(ASSIGN_COOKIES_TEST_CASES) | ||
| def test_find_content_children( | ||
| self, greed: List[int], cookies: List[int], expected: int | ||
| ): | ||
| actual = find_content_children(greed, cookies) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
+45.1 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+42.5 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+32.2 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+48.8 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+42.6 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+46.3 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+44 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+46.7 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+41.7 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+43.6 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
+41.8 KB
algorithms/greedy/gas_stations/images/solutions/gas_stations_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
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.