-
-
Notifications
You must be signed in to change notification settings - Fork 99
3720. Lexicographically Smallest Permutation Greater Than Target #182
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
Conversation
Reviewer's GuideThis PR adds a new C++ solution for LeetCode 3720 that computes the lexicographically smallest permutation of one string greater than a target by scanning pivot positions, matching prefixes, and constructing suffixes via frequency maps and map.upper_bound. Class diagram for Solution and lexGreaterPermutation methodclassDiagram
class Solution {
+string lexGreaterPermutation(string s, string target)
}
Flow diagram for lexGreaterPermutation algorithmflowchart TD
A["Start"] --> B["Initialize ans as empty string"]
B --> C["Build frequency map mp from s"]
C --> D["For i in 0 to n-1"]
D --> E["Copy mp to m2"]
E --> F["Build prefix: for j in 0 to i-1, match target[j] in m2"]
F --> G{Prefix matched?}
G -- No --> D
G -- Yes --> H["Find first char > target[i] in m2"]
H --> I{Found?}
I -- No --> D
I -- Yes --> J["Add char to result, update m2"]
J --> K["Append remaining chars in m2 in ascending order"]
K --> L["Update ans if result is smaller"]
L --> D
D --> M["Return ans"]
File-Level Changes
Possibly linked issues
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.
Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo
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.
Hey there - I've reviewed your changes - here's some feedback:
- Since the first valid break position produces the lexicographically smallest answer, you can return immediately when you construct it instead of tracking and comparing all candidates.
- Consider replacing the std::map<char,int> with a fixed‐size array (e.g., vector of size 26) to avoid repeated map copying and speed up lookups.
- Rename variables like mp, m2, ip, and res to more descriptive names to improve code readability and maintainability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since the first valid break position produces the lexicographically smallest answer, you can return immediately when you construct it instead of tracking and comparing all candidates.
- Consider replacing the std::map<char,int> with a fixed‐size array (e.g., vector<int> of size 26) to avoid repeated map copying and speed up lookups.
- Rename variables like mp, m2, ip, and res to more descriptive names to improve code readability and maintainability.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
All looks good to me..Star the repo ⭐ as well.. |
|
@SjxSubham I have starred the repo, Please merge my PR now. |
🎉 Congrats on getting your PR merged in, @Once-1296! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
PR Title Format: 3720. Lexicographically Smallest Permutation Greater Than Target
(https://leetcode.com/problems/lexicographically-smallest-permutation-greater-than-target/)
Intuition
Size of string is in [1,300]. Approach must be O(n^2) or O(n^2 logn).
When can a string be greater than another string of the same length?
if and only if for all characters at j in [0,i] s[j] = t[j]
and s[i+1]>t[i+1]
where i +1 < n (for proper indexing)
Approach
a. Try to match the first ith characters of s and target (0 to i-1 in 0 indexing)
b. for the ith (0 indexed) character of target, find the first character greater than it in the remaining frequency map
c. if no such character found continue
d. else for all indices from i +1 onwards till n-1, put all remaining chars of map in ascending order.
e. if ans is empty string, set ans as found string, else set ans as minimum between ans and found string
Code Solution (C++)
Related Issues
Closes Issue: #181
By submitting this PR, I confirm that:
Summary by Sourcery
New Features: