-
Notifications
You must be signed in to change notification settings - Fork 1
Add solutions and explanations for problems 3652, 3769, 3774, 3775, 3776, 3777 #123
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-3652-3705-3769-3774-3775-3776-3777
Dec 18, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We are given a binary string and can perform two types of operations: flip all characters from index 0 to i (cost: i+1) or flip all characters from index i to n-1 (cost: n-i). We need to find the minimum cost to make all characters equal. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
|
|
||
| - **Input Size:** The string length n can be up to 10^5, and each character is either '0' or '1'. | ||
| - **Time Complexity:** O(n) - we iterate through the string once to find all transitions. | ||
| - **Space Complexity:** O(1) - we only use a constant amount of extra space. | ||
| - **Edge Case:** If the string already has all characters equal, there are no transitions and the cost is 0. | ||
|
|
||
| **1.2 High-level approach:** | ||
|
|
||
| The goal is to find the minimum cost to eliminate all transitions (places where adjacent characters differ) in the string. When we encounter a transition, we can either flip the prefix or the suffix, and we choose the cheaper option. | ||
|
|
||
|  | ||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
|
|
||
| - **Brute Force:** Try all possible combinations of operations, which would be exponential O(2^n) and too slow. | ||
| - **Optimized Strategy:** For each transition at position i, we can either flip [0, i-1] (cost i) or flip [i, n-1] (cost n-i). We choose min(i, n-i) and sum all transition costs. This is O(n) time. | ||
| - **Optimization:** By recognizing that each transition can be fixed independently with the cheaper operation, we avoid exponential search and solve in linear time. | ||
|
|
||
| **1.4 Decomposition:** | ||
|
|
||
| 1. Iterate through the string from left to right. | ||
| 2. For each position i, check if there's a transition (s[i] != s[i-1]). | ||
| 3. If a transition exists, add the minimum cost (min(i, n-i)) to fix it. | ||
| 4. Return the total cost. | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
|
|
||
| Let's use the example: `s = "0011"`, `n = 4`. | ||
|
|
||
| - Initialize `res = 0` to track total cost. | ||
| - The string has one transition at position 2 (between '0' and '1'). | ||
|
|
||
| **2.2 Start Checking:** | ||
|
|
||
| We iterate through the string starting from index 1, comparing each character with the previous one. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | Position i | s[i] | s[i-1] | Transition? | Cost Option 1 (flip prefix) | Cost Option 2 (flip suffix) | Chosen Cost | res | | ||
| |------------|------|--------|-------------|---------------------------|----------------------------|-------------|-----| | ||
| | 1 | '0' | '0' | No | - | - | 0 | 0 | | ||
| | 2 | '1' | '0' | Yes | 2 | 2 | 2 | 2 | | ||
| | 3 | '1' | '1' | No | - | - | 0 | 2 | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
|
|
||
| After checking all positions, we have found all transitions and calculated the minimum cost to fix each one. | ||
|
|
||
| **2.5 Return Result:** | ||
|
|
||
| The result is 2, which is the minimum cost to make all characters equal. We achieve this by flipping the suffix from index 2 to the end, changing "0011" to "0000". | ||
|
|
||
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
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.
🧩 Analysis chain
🏁 Script executed:
Repository: romankurnovskii/leetcode-apps
Length of output: 121
🏁 Script executed:
Repository: romankurnovskii/leetcode-apps
Length of output: 1748
🌐 Web query:
LeetCode problem 3652💡 Result:
LeetCode 3652 — "Best Time to Buy and Sell Stock using Strategy"
Problem (brief):
Key idea / O(n) solution:
Δ = - (s[i] - s[i-k]) + (t[i] - t[i-k/2]).
Complexity:
Reference:
[1] LeetCode 3652 problem page / solution summary.
Explanation file contains wrong problem description.
The explanation file
explanations/3652/en.mddescribes a binary string flipping problem ("flip all characters from index 0 to i"), but LeetCode problem 3652 is "Best Time to Buy and Sell Stock using Strategy"—a stock trading profit maximization problem. The solution filesolutions/3652/01.pycorrectly implements the stock trading problem withmaxProfit(prices, strategy, k).Replace the explanation with content matching the actual problem: given arrays
prices,strategy(with values -1, 0, 1), and evenk, modify exactly one length-k consecutive window (first k/2 entries to 0, last k/2 to 1) to maximize profit where profit = sum(strategy[i] * prices[i]).🤖 Prompt for AI Agents