-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: add LeetCode problem 605 #1383
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
Closed
Closed
Changes from all commits
Commits
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,69 @@ | ||
| /** | ||
| * Determines if n new flowers can be planted in the flowerbed without violating | ||
| * the no-adjacent-flowers rule. Approach: Iterate through the flowerbed array, | ||
| * checking for spots where a flower can be planted (i.e., where the current | ||
| * spot and its adjacent spots are empty). Update the flowerbed array to reflect | ||
| * the planting and increment a counter. Stop if the required number of flowers | ||
| * (n) has been planted. | ||
| * | ||
| * Time Complexity: O(flowerbedSize), as we iterate through the flowerbed array | ||
| * once. Space Complexity: O(1), as we use a fixed amount of space regardless of | ||
| * the input size. | ||
| * | ||
| * @param flowerbed Pointer to the array representing the flowerbed, where 0 | ||
| * indicates an empty spot and 1 indicates a flower. | ||
| * @param flowerbedSize The size of the flowerbed array. | ||
| * @param n The number of flowers we wish to plant. | ||
| * @return True if it is possible to plant n flowers without violating the rule | ||
| * that no two flowers can be adjacent, otherwise false. | ||
| */ | ||
| bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) | ||
| { | ||
| // Handle edge case: single empty spot in flowerbed | ||
| if (flowerbedSize == 1 && flowerbed[0] == 0) | ||
| return true; | ||
|
|
||
| int count = 0; // Counter for the number of flowers planted | ||
| for (int i = 0; i < flowerbedSize; i++) | ||
| { | ||
| // Check if current spot is empty | ||
| if (flowerbed[i] != 1) | ||
| { | ||
| if (i == 0) | ||
| { | ||
| // Special case for the first element: check if the next element | ||
| // is empty | ||
| if (flowerbed[i + 1] == 0) | ||
| { | ||
| flowerbed[i] = 1; // Plant flower | ||
| count++; | ||
| } | ||
| } | ||
| else if (i == flowerbedSize - 1) | ||
| { | ||
| // Special case for the last element: check if the previous | ||
| // element is empty | ||
| if (flowerbed[i - 1] == 0) | ||
| { | ||
| flowerbed[i] = 1; // Plant flower | ||
| count++; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // For middle elements, check both previous and next elements | ||
| // are empty | ||
| if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) | ||
| { | ||
| flowerbed[i] = 1; // Plant flower | ||
| count++; | ||
| } | ||
| } | ||
| } | ||
| // Check if the required number of flowers has been planted | ||
| if (count >= n) | ||
| return true; | ||
| } | ||
| // Return false if unable to plant the required number of flowers | ||
| return false; | ||
| } | ||
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.