-
Notifications
You must be signed in to change notification settings - Fork 1
Add solutions and explanations for problems 2390, 2542, 3753, 3759-3762, 3765-3772 #105
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 1 commit into
main
from
problems-2390-2542-3753-3759-3760-3761-3762-3765-3766-3767-3768-3769-3770-3771-3772
Dec 8, 2025
Merged
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
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,59 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We need to remove stars from a string. Each star removes the closest non-star character to its left, along with the star itself. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `1 <= s.length <= 10^5` | ||
| - **Time Complexity:** O(n) where n is the length of the string, as we process each character once | ||
| - **Space Complexity:** O(n) for the stack to store characters | ||
| - **Edge Case:** If the string contains only stars, all characters will be removed, resulting in an empty string | ||
|
|
||
| **1.2 High-level approach:** | ||
| We use a stack to simulate the removal process. When we encounter a star, we pop the most recent character (the one to the left). When we encounter a non-star, we push it onto the stack. | ||
|
|
||
|  | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** For each star, find and remove the closest non-star to the left by scanning backwards, which would be O(n²) in worst case | ||
| - **Optimized Strategy:** Use a stack to maintain characters, allowing O(1) removal of the most recent character, resulting in O(n) time | ||
| - **Emphasize the optimization:** The stack allows us to efficiently track and remove the "closest left" character without scanning | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Initialize an empty stack to store characters | ||
| 2. Iterate through each character in the string | ||
| 3. If the character is a star, remove the top element from the stack (if it exists) | ||
| 4. If the character is not a star, add it to the stack | ||
| 5. Convert the remaining stack elements to a string and return | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `s = "leet**cod*e"` | ||
| - Initialize an empty stack: `stack = []` | ||
|
|
||
| **2.2 Start Processing:** | ||
| We iterate through each character in the string from left to right. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Character | Stack Before | Action | Stack After | | ||
| |-----------|--------------|--------|-------------| | ||
| | 'l' | [] | Push 'l' | ['l'] | | ||
| | 'e' | ['l'] | Push 'e' | ['l', 'e'] | | ||
| | 'e' | ['l', 'e'] | Push 'e' | ['l', 'e', 'e'] | | ||
| | 't' | ['l', 'e', 'e'] | Push 't' | ['l', 'e', 'e', 't'] | | ||
| | '*' | ['l', 'e', 'e', 't'] | Pop 't' | ['l', 'e', 'e'] | | ||
| | '*' | ['l', 'e', 'e'] | Pop 'e' | ['l', 'e'] | | ||
| | 'c' | ['l', 'e'] | Push 'c' | ['l', 'e', 'c'] | | ||
| | 'o' | ['l', 'e', 'c'] | Push 'o' | ['l', 'e', 'c', 'o'] | | ||
| | 'd' | ['l', 'e', 'c', 'o'] | Push 'd' | ['l', 'e', 'c', 'o', 'd'] | | ||
| | '*' | ['l', 'e', 'c', 'o', 'd'] | Pop 'd' | ['l', 'e', 'c', 'o'] | | ||
| | 'e' | ['l', 'e', 'c', 'o'] | Push 'e' | ['l', 'e', 'c', 'o', 'e'] | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| After processing all characters, we continue to the next step. | ||
|
|
||
| **2.5 Return Result:** | ||
| The final stack contains `['l', 'e', 'c', 'o', 'e']`, which when joined gives `"lecoe"`, which is the result. |
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,52 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We need to choose a subsequence of k indices from nums1, and our score is the sum of selected nums1 values multiplied by the minimum of selected nums2 values. We want to maximize this score. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `1 <= n <= 10^5` | ||
| - **Time Complexity:** O(n log n) for sorting + O(n log k) for heap operations = O(n log n) | ||
| - **Space Complexity:** O(k) for the heap | ||
| - **Edge Case:** When k = 1, we simply find the maximum of nums1[i] * nums2[i] | ||
|
|
||
| **1.2 High-level approach:** | ||
| We sort pairs by nums2 in descending order. As we process each pair, we maintain a min-heap of the k largest nums1 values. The current nums2 value becomes the minimum for our current selection, and we calculate the score. | ||
|
|
||
|  | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** Try all C(n,k) combinations, which is exponential | ||
| - **Optimized Strategy:** Sort by nums2 descending, use heap to maintain top k nums1 values, achieving O(n log n) time | ||
| - **Emphasize the optimization:** By sorting by nums2, we ensure that when we process a pair, its nums2 value is the minimum in our current selection | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Create pairs of (nums1[i], nums2[i]) and sort by nums2 in descending order | ||
| 2. Use a min-heap to maintain the k largest nums1 values seen so far | ||
| 3. For each pair, add nums1 to heap, maintain heap size k, calculate score | ||
| 4. Track the maximum score encountered | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `nums1 = [1,3,3,2]`, `nums2 = [2,1,3,4]`, `k = 3` | ||
| - Sort pairs by nums2 descending: [(2,4), (3,3), (1,2), (3,1)] | ||
| - Initialize heap and sum: `heap = []`, `current_sum = 0` | ||
|
|
||
| **2.2 Start Processing:** | ||
| We iterate through sorted pairs. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Pair | Heap Before | Action | Heap After | Sum | Score | Max Score | | ||
| |------|-------------|--------|------------|-----|-------|-----------| | ||
| | (2,4) | [] | Push 2 | [2] | 2 | 2*4=8 | 8 | | ||
| | (3,3) | [2] | Push 3 | [2,3] | 5 | 5*3=15 | 15 | | ||
| | (1,2) | [2,3] | Push 1, Pop 1 | [2,3] | 5 | 5*2=10 | 15 | | ||
| | (3,1) | [2,3] | Push 3, Pop 2 | [3,3] | 6 | 6*1=6 | 15 | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| After processing all pairs, we continue tracking the maximum. | ||
|
|
||
| **2.5 Return Result:** | ||
| The maximum score encountered is 15, which is the result. |
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,51 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** A number is a "Complete Prime Number" if every prefix and every suffix of the number is prime. We need to check if a given number satisfies this condition. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `1 <= num <= 10^9` | ||
| - **Time Complexity:** O(d * sqrt(n)) where d is the number of digits, as we check primality for each prefix and suffix | ||
| - **Space Complexity:** O(d) to store the string representation | ||
| - **Edge Case:** Single-digit numbers are complete prime only if they are prime themselves | ||
|
|
||
| **1.2 High-level approach:** | ||
| We convert the number to a string, then check if all prefixes (from left) and all suffixes (from right) are prime numbers. | ||
|
|
||
|  | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** Check all prefixes and suffixes for primality, which is what we do | ||
| - **Optimized Strategy:** Use efficient primality testing (trial division up to sqrt(n)), achieving reasonable performance | ||
| - **Emphasize the optimization:** We only need to check divisors up to sqrt(n) for primality testing | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Convert the number to a string to access individual digits | ||
| 2. Check all prefixes: from first digit, first two digits, ..., all digits | ||
| 3. Check all suffixes: from last digit, last two digits, ..., all digits | ||
| 4. Return true only if all prefixes and suffixes are prime | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `num = 23` | ||
| - Convert to string: `s = "23"` | ||
|
|
||
| **2.2 Start Checking:** | ||
| We check all prefixes first, then all suffixes. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Type | Substring | Value | Is Prime? | | ||
| |------|-----------|-------|-----------| | ||
| | Prefix | "2" | 2 | Yes | | ||
| | Prefix | "23" | 23 | Yes | | ||
| | Suffix | "3" | 3 | Yes | | ||
| | Suffix | "23" | 23 | Yes | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| After checking all prefixes and suffixes, we verify all are prime. | ||
|
|
||
| **2.5 Return Result:** | ||
| All prefixes and suffixes are prime, so the result is `true`. |
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,50 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** For each number in the array, we need to find the minimum number of operations (increment or decrement by 1) to make it a binary palindrome. A binary palindrome is a number whose binary representation reads the same forward and backward. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `1 <= nums.length <= 5000`, `1 <= nums[i] <= 5000` | ||
| - **Time Complexity:** O(p * n) where p is the number of palindromes (about 100) and n is array length | ||
| - **Space Complexity:** O(p) to store precomputed palindromes | ||
| - **Edge Case:** If a number is already a binary palindrome, it requires 0 operations | ||
|
|
||
| **1.2 High-level approach:** | ||
| We precompute all binary palindromes up to 5000, then for each number, find the closest palindrome and calculate the minimum operations needed. | ||
|
|
||
|  | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** For each number, check all nearby numbers for palindromes, which could be inefficient | ||
| - **Optimized Strategy:** Precompute all palindromes once, then use binary search or linear scan to find closest, achieving O(p * n) time | ||
| - **Emphasize the optimization:** Precomputation allows us to quickly find the closest palindrome for each number | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Precompute all binary palindromes up to the maximum possible value (5000) | ||
| 2. For each number in the input array, find the closest palindrome | ||
| 3. Calculate the absolute difference as the minimum operations | ||
| 4. Return the array of minimum operations | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `nums = [1,2,4]` | ||
| - Precompute palindromes: [1, 3, 5, 7, 9, 15, ...] (numbers with palindromic binary) | ||
|
|
||
| **2.2 Start Processing:** | ||
| We iterate through each number and find its closest palindrome. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Number | Binary | Closest Palindrome | Operations | Result | | ||
| |--------|--------|-------------------|------------|--------| | ||
| | 1 | 1 | 1 | |0| = 0 | 0 | | ||
| | 2 | 10 | 3 (11) | |2-3| = 1 | 1 | | ||
| | 4 | 100 | 3 (11) | |4-3| = 1 | 1 | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| After processing all numbers, we have the results. | ||
|
|
||
| **2.5 Return Result:** | ||
| The result array is `[0, 1, 1]`, representing the minimum operations for each number. | ||
Oops, something went wrong.
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.
Fix table formatting and clarify operations expressions
The absolute-value notation in the Operations column introduces extra
|characters, breaking the table (and triggering MD056), and the first row’s formula is a bit unclear.You can avoid both issues by rewriting the Operations cells without
|characters:This keeps the table well-formed and makes the math explicit.
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
42-42: Table column count
Expected: 5; Actual: 7; Too many cells, extra data will be missing
(MD056, table-column-count)
43-43: Table column count
Expected: 5; Actual: 7; Too many cells, extra data will be missing
(MD056, table-column-count)
44-44: Table column count
Expected: 5; Actual: 7; Too many cells, extra data will be missing
(MD056, table-column-count)
🤖 Prompt for AI Agents