-
Notifications
You must be signed in to change notification settings - Fork 1
Add solutions and explanations for problems 955, 3784, 3783, 3781, 3780, 3779, 3776, 3775, 3774, 3716 #127
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
romankurnovskii
merged 3 commits into
main
from
problems-955-3784-3783-3781-3780-3779-3776-3775-3774-3716
Dec 21, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| npm run format:json | ||
| npm run format:python |
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
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
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,68 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We are given an integer array. In one operation, we remove the first three elements (or all remaining if fewer than three). We repeat until the array is empty or contains no duplicate values. We need to find the minimum number of operations required. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
|
|
||
| - **Input Size:** Up to 10^5 elements in the array. | ||
| - **Time Complexity:** O(n) where n is the length of the array - we process from right to left once. | ||
| - **Space Complexity:** O(n) to store the set of seen elements. | ||
| - **Edge Case:** If all elements are already distinct, we return 0 without any operations. | ||
|
|
||
| **1.2 High-level approach:** | ||
|
|
||
| The goal is to process the array from right to left, tracking which elements we've seen. If we encounter a duplicate while going right to left, we know we need to remove elements from the left. The number of operations needed is calculated based on how many elements remain. | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
|
|
||
| - **Brute Force:** Simulate the operations step by step, removing first 3 elements each time and checking for duplicates. This could be O(n^2) in worst case. | ||
| - **Optimized Strategy:** Process from right to left, use a set to track seen elements. When we find a duplicate, calculate operations needed: (remaining elements + 2) // 3. This is O(n) time. | ||
| - **Optimization:** By processing from right to left, we can determine the answer without actually performing the removals, making the solution much more efficient. | ||
|
|
||
| **1.4 Decomposition:** | ||
|
|
||
| 1. Process the array from right to left. | ||
| 2. Use a set to track elements we've seen. | ||
| 3. For each element from right to left: | ||
| - If it's already in the set, we found a duplicate. | ||
| - Calculate operations needed: (remaining elements + 2) // 3. | ||
| - Return the calculated operations. | ||
| 4. If no duplicates found, return 0. | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
|
|
||
| Let's use the example: `nums = [3, 8, 3, 6, 5, 8]` | ||
|
|
||
| - Start from right: `8` (last element) | ||
| - Seen set: `{8}` | ||
| - Process right to left | ||
|
|
||
| **2.2 Start Processing:** | ||
|
|
||
| We begin processing from the rightmost element, adding each to the seen set. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Step | Element | In Seen? | Action | Seen Set | Remaining Elements | | ||
| | ---- | ------- | -------- | ------ | -------- | ------------------ | | ||
| | 1 | 8 (rightmost) | No | Add to seen | `{8}` | 5 | | ||
| | 2 | 5 | No | Add to seen | `{8, 5}` | 4 | | ||
| | 3 | 6 | No | Add to seen | `{8, 5, 6}` | 3 | | ||
| | 4 | 3 | No | Add to seen | `{8, 5, 6, 3}` | 2 | | ||
| | 5 | 8 | Yes | Found duplicate! | `{8, 5, 6, 3}` | 2 | | ||
| | 6 | Calculate | - | `(2 + 2) // 3 = 1` | - | - | | ||
|
|
||
| When we find duplicate 8 at position 1 (0-indexed), we have 2 elements remaining (indices 0 and 1). Operations needed: (2 + 2) // 3 = 1. | ||
|
|
||
| **2.4 Increment and Loop:** | ||
|
|
||
| We continue processing from right to left until we find a duplicate or finish processing all elements. | ||
|
|
||
| **2.5 Return Result:** | ||
|
|
||
| The result is 1, meaning we need 1 operation to remove the first 3 elements, leaving `[6, 5, 8]` which are all distinct. | ||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space complexity is inconsistent with the implementation.
The explanation states O(1) space, but the solution creates a copy with
A = balance[:], which is O(n) space. Consider updating the documentation to reflect the actual space usage.🔎 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents