-
Notifications
You must be signed in to change notification settings - Fork 1
Add solutions and explanations for problems 3722, 3727, 3759 #113
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
Add solutions and explanations for problems 3722, 3727, 3759 #113
Conversation
…raphically Smallest String After Reverse, Maximum Alternating Sum of Squares, Count Elements With at Least K Greater Values
WalkthroughThis PR removes documentation for problem 1431 from multiple markdown files, adds problem 3583 to the problem set, and rewrites/refactors explanations and solutions for problems 3722, 3727, and 3759. Problem 3759's solution undergoes a complete algorithmic change from Floyd-Warshall graph computation to binary-search-based counting. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Reviewer's GuideImplements accepted solutions and refreshed explanations for LeetCode problems 3722, 3727, and 3759, refactors the corresponding Python implementations for clarity/efficiency, and removes duplicated content for problem 1431 from book indices. Class diagram for new LeetCode Solution implementationsclassDiagram
class Solution3722 {
+str lexSmallest(str s)
}
class Solution3727 {
+int maxAlternatingSum(List_int nums)
}
class Solution3759 {
+int countElements(List_int nums, int k)
}
%% Represent logical grouping only; actual code uses separate Solution classes per file
Solution3722 ..> Solution3727 : independent_solutions
Solution3727 ..> Solution3759 : independent_solutions
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
solutions/3727/01.py (1)
14-19: Remove unused variablenum_negative.The variable
num_negativeis calculated but never used. The slicingsquares[num_positive:]on line 19 implicitly handles the negative positions.Apply this diff:
- # Count how many positive and negative positions we have num_positive = (n + 1) // 2 # Even indices: 0, 2, 4, ... - num_negative = n // 2 # Odd indices: 1, 3, 5, ... # Assign largest squares to positive positions, smallest to negative res = sum(squares[:num_positive]) - sum(squares[num_positive:])Based on static analysis hints.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
books/All.md(0 hunks)books/LeetCode_75.md(0 hunks)books/Visualization.md(0 hunks)data/book-sets.json(1 hunks)explanations/3722/en.md(1 hunks)explanations/3727/en.md(1 hunks)explanations/3759/en.md(1 hunks)solutions/3722/01.py(1 hunks)solutions/3727/01.py(1 hunks)solutions/3759/01.py(1 hunks)
💤 Files with no reviewable changes (3)
- books/Visualization.md
- books/All.md
- books/LeetCode_75.md
🧰 Additional context used
🪛 LanguageTool
explanations/3722/en.md
[style] ~20-~20: Consider an alternative for the overused word “exactly”.
Context: ...y with the current best result. This is exactly what we do, and it's efficient enough g...
(EXACTLY_PRECISELY)
🪛 markdownlint-cli2 (0.18.1)
explanations/3727/en.md
5-5: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
7-7: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
8-8: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
9-9: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
10-10: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
12-12: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
18-18: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
20-20: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
21-21: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
23-23: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
33-33: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
39-39: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
43-43: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
61-61: Emphasis used instead of a heading
(MD036, no-emphasis-as-heading)
🪛 Ruff (0.14.8)
solutions/3727/01.py
16-16: Local variable num_negative is assigned to but never used
Remove assignment to unused variable num_negative
(F841)
🔇 Additional comments (7)
data/book-sets.json (1)
64-66: LGTM! Problem 3583 correctly added to the problem set.The addition of problem 3583 to the "All" problems list is correctly positioned in sorted order before 3602.
solutions/3722/01.py (1)
1-22: LGTM! Clear variable naming improves readability.The refactoring enhances code clarity without changing functionality:
reversed_prefixandreversed_suffixare more descriptive than the genericreversed_str- The
candidatevariable makes the comparison logic easier to follow- Algorithm correctly implements the brute force approach with O(n²) time complexity
explanations/3722/en.md (1)
1-67: LGTM! Comprehensive and accurate documentation.The explanation clearly describes the algorithm with:
- Correct complexity analysis (O(n²) time, O(n) space)
- Detailed trace walkthrough showing prefix and suffix reversals
- Accurate example demonstrating the solution ("dcab" → "acdb")
solutions/3727/01.py (1)
1-21: LGTM! Correct greedy algorithm implementation.The solution correctly implements the optimal strategy:
- Squares all numbers and sorts in descending order
- Assigns largest squares to positive positions (even indices)
- Computes the alternating sum efficiently using prefix/suffix sums
- Time complexity O(n log n) as stated in PR objectives
solutions/3759/01.py (1)
1-38: LGTM! Correct implementation of counting algorithm.The solution correctly implements the counting approach:
- Sorts the array for efficient binary search
- Iterates through distinct values with occurrence counting
- Uses binary search to find the upper bound (first element strictly greater)
- Correctly counts elements with at least k greater values
- Time complexity O(n log n) as stated in PR objectives
The binary search implementation is correct for finding the first index with a value strictly greater than the current value.
explanations/3727/en.md (1)
1-61: LGTM! Clear and accurate documentation.The explanation correctly describes the algorithm:
- Accurate complexity analysis (O(n log n) time, O(n) space)
- Clear high-level approach with greedy optimization strategy
- Detailed trace walkthrough with correct result (12)
- Example properly demonstrates sorting and position assignment
explanations/3759/en.md (1)
1-58: LGTM! Comprehensive and accurate documentation.The explanation correctly describes the counting algorithm:
- Accurate complexity analysis (O(n log n) time, O(n) space)
- Clear explanation of the sorting and binary search approach
- Detailed trace walkthrough with correct result (2)
- Proper handling of distinct values and upper bound search
Summary
This PR adds solutions and explanations for 3 LeetCode problems that passed all test cases:
Problem 3722: Lexicographically Smallest String After Reverse
Problem 3727: Maximum Alternating Sum of Squares
Problem 3759: Count Elements With at Least K Greater Values
Files Added
solutions/3722/01.pyexplanations/3722/en.mdsolutions/3727/01.pyexplanations/3727/en.mdsolutions/3759/01.pyexplanations/3759/en.mdAll solutions follow the repository's coding standards and include detailed explanations following the template structure.
Summary by Sourcery
Add and refine solutions and explanations for LeetCode problems 3722, 3727, and 3759 while cleaning up outdated/duplicated documentation entries.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Summary by CodeRabbit
Documentation
New Features
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.