-
Notifications
You must be signed in to change notification settings - Fork 2
feat(puzzles, math): rectangle area and max consecutive ones #117
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
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,3 +20,36 @@ def longest_ones(nums: List[int], k: int) -> int: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| left += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return right - left + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def find_max_consecutive_ones(nums: List[int]) -> int: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Finds the maximum consecutive ones in a binary array and returns it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The most straightforward way to solve this is to use a single pass through the array, keeping track of two values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| as we go. First, we need a counter for the current streak of consecutive 1s we're seeing. Second, we need to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| remember the maximum streak we've encountered so far. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| As we examine each element, if we see a 1, we increment our current streak counter. If we see a 0, that breaks our | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| streak, so we reset the counter to 0. Importantly, every time we update our current streak, we check whether it's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| larger than our maximum and update the maximum if needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Time complexity is O(n) where n is the length of the input array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Space complexity is O(1) as no extra space is required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nums(list): a list of 1s and 0s. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int: maximum number of consecutive 1s in the nums binary array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+43
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. Add reference URL as per PR checklist. The PR checklist confirms "All new algorithms include a URL in comments pointing to Wikipedia or similar," but no reference URL is present for this algorithm. Consider adding a comment with a link to the problem description or relevant documentation. For example: +# Reference: https://leetcode.com/problems/max-consecutive-ones/
+# or https://en.wikipedia.org/wiki/Longest_run
def find_max_consecutive_ones(nums: List[int]) -> int:
"""
Finds the maximum consecutive ones in a binary array and returns it.📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(nums) == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_ones = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_consecutive = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for num in nums: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if num == 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_consecutive += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_ones = max(max_ones, current_consecutive) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_consecutive = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return max_ones | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Rectangle Area | ||
|
|
||
| You are given the coordinates of two axis-aligned rectangles in a 2D plane. Your task is to calculate the total area | ||
| covered by both rectangles. | ||
|
|
||
| The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2). | ||
|
|
||
| Similarly, the second rectangle is defined by its bottom-left corner (bx1, by1) and top-right corner (bx2, by2). | ||
|
Comment on lines
+3
to
+8
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 coordinate typo in rectangle definition (ax2 vs ay1). Line 6 currently says top-right is -The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ay1, ay2).
+The first rectangle is specified by the coordinates of its bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2).Also applies to: 14-17 🤖 Prompt for AI Agents |
||
|
|
||
| > Note: The rectangles may overlap. | ||
| ## Constraints | ||
|
|
||
| 1. -10^4 ≤ ax1 ≤ ax2 ≤ 10^4 | ||
| 2. −10^4 ≤ ay1 ≤ ay2 ≤ 10^4 | ||
| 3. −10^4 ≤ bx1 ≤ bx2 ≤10^4 | ||
| 4. −10^4 ≤ by1 ≤ by2 ≤ 10^4 | ||
|
|
||
| ## Examples | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| def compute_area( | ||
| ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int | ||
| ): | ||
| # 1. Calculate the area of each individual rectangle | ||
| area_a = (ax2 - ax1) * (ay2 - ay1) | ||
| area_b = (bx2 - bx1) * (by2 - by1) | ||
|
|
||
| # 2. Calculate the area of the overlap (A intersect B) | ||
|
|
||
| # Determine the coordinates of the overlap rectangle: (ix1, iy1) to (ix2, iy2) | ||
|
|
||
| # The left edge of the overlap is the max of the two left edges | ||
| ix1 = max(ax1, bx1) | ||
| # The right edge of the overlap is the min of the two right edges | ||
| ix2 = min(ax2, bx2) | ||
|
|
||
| # The bottom edge of the overlap is the max of the two bottom edges | ||
| iy1 = max(ay1, by1) | ||
| # The top edge of the overlap is the min of the two top edges | ||
| iy2 = min(ay2, by2) | ||
|
|
||
| # Calculate the width and height of the overlap | ||
| overlap_width = max(0, ix2 - ix1) | ||
| overlap_height = max(0, iy2 - iy1) | ||
|
|
||
| # The max(0, ...) ensures that if the rectangles do not overlap | ||
| # (e.g., ix2 < ix1), the width/height is 0, and the overlap_area is 0. | ||
| overlap_area = overlap_width * overlap_height | ||
|
|
||
| # 3. Apply the Inclusion-Exclusion Principle | ||
| total_area = area_a + area_b - overlap_area | ||
|
|
||
| return total_area | ||
|
Comment on lines
+1
to
+33
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. Add return type + docstring/doctest + reference URL (per repo checklist).
-def compute_area(
+def compute_area(
ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int
-):
+) -> int:
+ """
+ Compute total area covered by two axis-aligned rectangles.
+
+ Reference: https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle
+
+ >>> compute_area(0, 0, 2, 2, 1, 1, 3, 3)
+ 7
+ """(Optional hardening): consider guarding against reversed coordinates (e.g., |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from pymath.rectangle_area import compute_area | ||
|
|
||
|
|
||
| class RectangleAreaTestCase(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| (0, 0, 1, 1, 2, 2, 3, 3, 2), | ||
| (0, 0, 2, 2, 1, 1, 3, 3, 7), | ||
| (-1, -1, 2, 2, 0, 0, 1, 1, 9), | ||
| (0, 0, 0, 0, 1, 1, 2, 2, 1), | ||
| (-8918, -419, -7715, 577, -8918, -419, -7715, 577, 1198188), | ||
| ] | ||
| ) | ||
| def test_compute_area( | ||
| self, | ||
| ax1: int, | ||
| ay1: int, | ||
| ax2: int, | ||
| ay2: int, | ||
| bx1: int, | ||
| by1: int, | ||
| bx2: int, | ||
| by2: int, | ||
| expected, | ||
| ): | ||
| actual = compute_area(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.