-
Notifications
You must be signed in to change notification settings - Fork 1
LeetCode-2975: Maximum Square Area by Removing Fences From a Field #116
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| # https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field | ||
| # 2975. Maximum Square Area by Removing Fences From a Field | ||
| def maximizeSquareArea(self, m: int, n: int, hFences: List[int], vFences: List[int]) -> int: | ||
| # Add boundaries | ||
| """ | ||
| This function calculates the maximum square area that can be formed by removing fences from a field of size m x n. | ||
|
|
||
| Parameters: | ||
| m (int): The width of the field | ||
| n (int): The height of the field | ||
| hFences (List[int]): A list of horizontal fence positions | ||
| vFences (List[int]): A list of vertical fence positions | ||
|
|
||
| Returns: | ||
| int: The maximum square area that can be formed, or -1 if it is not possible | ||
| """ | ||
| h = sorted([1] + hFences + [m]) | ||
| v = sorted([1] + vFences + [n]) | ||
|
|
||
| # possible horizontal distances | ||
| h_dist = set() | ||
| for i in range(len(h)): | ||
| for j in range(i + 1, len(h)): | ||
| h_dist.add(h[j] - h[i]) | ||
| max_dist = 0 | ||
| # possible vertical distance | ||
| for i in range(len(v)): | ||
| for j in range(i + 1, len(v)): | ||
| d = v[j] - v[i] | ||
| if d in h_dist: | ||
| max_dist = max(d, max_dist) | ||
|
|
||
| MOD = 10**9 + 7 | ||
| return -1 if max_dist == 0 else (max_dist * max_dist) % MOD | ||
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.