Merged
Conversation
Reviewer's GuideAdds a C++ solution for LeetCode problem 57 by converting the insert-interval task into a standard merge-intervals workflow: appending the new interval, sorting all intervals, and merging overlaps in a single pass. Class diagram for the new Solution class (Insert Interval)classDiagram
class Solution {
+vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval)
+vector<vector<int>> merge(vector<vector<int>>& intervals)
}
Flow diagram for the Insert Interval algorithmflowchart TD
A["Start with intervals and newInterval"] --> B["Append newInterval to intervals"]
B --> C["Sort intervals by start time"]
C --> D["Initialize ans with first interval"]
D --> E["Iterate through intervals (from second)"]
E --> F{Is current interval overlapping with last in ans?}
F -- No --> G["Add current interval to ans"]
F -- Yes --> H["Merge: update end of last interval in ans"]
G --> I["Continue iteration"]
H --> I
I --> J["Return ans (merged intervals)"]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
Author
|
hello again @SjxSubham, i've raised the issue and made the pr, pls review and merge it, don't forget to assign the hacktoberfest-accepted label!! thank you.. |
SjxSubham
approved these changes
Oct 29, 2025
🎉 Congrats on getting your PR merged in, @ajithh404! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Title Format: 57.Insert Interval.cpp
Intuition
The key idea is to simplify the insert interval problem by converting it into a standard merge intervals problem.
Instead of figuring out where the new interval fits manually, we can:
This ensures all possible overlaps (including those created by the new interval) are correctly merged without needing separate insertion logic.
Approach
Append the new interval:
Add
newIntervalto the list of existing intervals.Sort the intervals:
Sort all intervals based on their starting points. This step ensures the merging process works linearly in one pass.
Merge overlapping intervals:
answith the first interval.ans.answith the maximum of both end times.Return the merged list:
After processing all intervals,
answill contain the correct set of non-overlapping intervals.Time Complexity:
Space Complexity:
Code Solution (C++)
Related Issues
Closes #264
By submitting this PR, I confirm that:
Summary by Sourcery
New Features: