-
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
base: main
Are you sure you want to change the base?
Changes from all commits
3cab43b
4f77531
eff6568
23a91a1
dbcbcb5
fae454f
b88d25b
1e5bdc7
635dff6
b71f628
d374048
510b0ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,5 +126,3 @@ Output: 6 | |
| - Arrays | ||
| - Dynamic Programming | ||
| - Greedy | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,18 +5,23 @@ | |||||||||||||||||||||||||||||||
| def max_profit(prices: List[int]) -> int: | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| Find the maximum profit that can be made from buying and selling a stock once | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Time complexity is O(n) as we iterate through each price to get the maximum profit | ||||||||||||||||||||||||||||||||
| Space complexity is O(1) as no extra space is used | ||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||
| prices(list): list of prices | ||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||
| int: maximum profit that can be made | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| if prices is None or len(prices) < 2: | ||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| start_price = prices[0] | ||||||||||||||||||||||||||||||||
| current_max_profit = 0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for price in prices: | ||||||||||||||||||||||||||||||||
| if price < start_price: | ||||||||||||||||||||||||||||||||
| start_price = price | ||||||||||||||||||||||||||||||||
| elif price - start_price > current_max_profit: | ||||||||||||||||||||||||||||||||
| current_max_profit = price - start_price | ||||||||||||||||||||||||||||||||
| for price in prices[1:]: | ||||||||||||||||||||||||||||||||
| start_price = min(start_price, price) | ||||||||||||||||||||||||||||||||
| current_max_profit = max(current_max_profit, price - start_price) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return current_max_profit | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -28,13 +33,14 @@ def max_profit_two_pointers(prices: List[int]) -> int: | |||||||||||||||||||||||||||||||
| Space: O(1), no extra memory is used | ||||||||||||||||||||||||||||||||
| Time: O(n), where n is the size of the input list & we iterate through the list only once | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| if prices is None or len(prices) < 2: | ||||||||||||||||||||||||||||||||
| number_of_prices = len(prices) | ||||||||||||||||||||||||||||||||
| if prices is None or number_of_prices < 2: | ||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Calling 🐛 Proposed fix def max_profit_two_pointers(prices: List[int]) -> int:
"""
Variation of max_profit using 2 pointers
Complexity Analysis:
Space: O(1), no extra memory is used
Time: O(n), where n is the size of the input list & we iterate through the list only once
"""
+ if prices is None or len(prices) < 2:
+ return 0
+
number_of_prices = len(prices)
- if prices is None or number_of_prices < 2:
- return 0
left, right = 0, 1Alternatively, keep - number_of_prices = len(prices)
- if prices is None or number_of_prices < 2:
+ if prices is None:
+ return 0
+ number_of_prices = len(prices)
+ if number_of_prices < 2:
return 0📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| left, right = 0, 1 | ||||||||||||||||||||||||||||||||
| current_max_profit = 0 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| while right < len(prices): | ||||||||||||||||||||||||||||||||
| while right < number_of_prices: | ||||||||||||||||||||||||||||||||
| low = prices[left] | ||||||||||||||||||||||||||||||||
| high = prices[right] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||
|
Comment on lines
+1
to
+3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Polish wording/typos in intro and solution. Consider a more formal opener and fix the spelling of “Intuitively”. ✏️ Suggested edits-Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most
+Assume you are a caring parent and want to give your children some cookies. But, you should give each child at most
...
-Intiutively, we want to give each child the smallest cookie that satisfies them.
+Intuitively, we want to give each child the smallest cookie that satisfies them.Also applies to: 46-47 🧰 Tools🪛 LanguageTool[style] ~3-~3: Consider using a more formal and expressive alternative to ‘awesome’. (AWESOME) 🤖 Prompt for AI Agents |
||||||||||||||
| 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 | ||||||||||||||
|
Comment on lines
+12
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the max-value constraint. “231 - 1” reads as 231−1 rather than 2^31−1. ✏️ Suggested fix-- 1 <= greed[i], cookies[j] <= 231 - 1
+- 1 <= greed[i], cookies[j] <= 2^31 - 1📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| ## 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. | ||||||||||||||
| 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 |
| 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() |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,3 +42,54 @@ Therefore, you can't travel around the circuit once no matter where you start. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Greedy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ## Solution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If there is more gas along the route than the cost of the route, then there is guaranteed to be a solution to the problem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| So the first step is to check if the sum of the gas is greater than or equal to the sum of the cost. If it is not, then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| we return -1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Next, we iterate through the gas station to find the starting index of our circuit using a greedy approach: whenever we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| don't have enough gas to reach the next station, we move our starting gas station to the next station and reset our gas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tank. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| We start at the first station, and fill our tank with gas[0] = 5 units of gas. From there, it takes cost[0] = 1 units of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gas to travel to the next station, so we arrive at station 2 (index 1) with 4 units of gas. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| At station 2, we fill our tank with gas[1] = 2 units of gas, for a total of 6 units of gas. It takes cost[1] = 5 units | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| of gas to travel to the next station, so we arrive at station 3 with 1 unit of gas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Now at station 3, we fill our tank with gas[2] = 0 units of gas, for a total of 1 unit of gas. It takes cost[2] = 5 units | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| of gas to travel to the next station, which we don't have. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This is where our greedy approach comes in. We reset our starting station to the next station i + 1 and reset our gas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tank to 0. We can do this because all other start indexes between 0 and 2 will also run into the same problem of not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| having enough gas to reach the next station, so we can rule them out. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+75
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarify the walkthrough input arrays to avoid confusion. The walkthrough’s numbers don’t match Example 1 earlier. Please explicitly state the arrays used (or align the walkthrough to Example 1) so readers don’t think the math is inconsistent. ✅ Suggested edit- We start at the first station, and fill our tank with gas[0] = 5 units of gas. From there, it takes cost[0] = 1 units of
+ Walkthrough below uses gas = [5, 2, 0, 3, 3] and cost = [1, 5, 5, 1, 1].
+ We start at station 1 (index 0), and fill our tank with gas[0] = 5 units of gas. From there, it takes cost[0] = 1 units of📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If we follow this approach of resetting the start index and gas tank whenever we don't have enough gas to reach the next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| station, then when we finish iterating, the last start index will be the solution to the problem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### Complexity Analysis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #### Time Complexity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| O(n) where n is the number of gas stations. We only iterate through the gas stations once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #### Space Complexity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| O(1) We only use constant extra space for variables. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.