DEV Community

Sameer Chawla
Sameer Chawla

Posted on

๐Ÿง  LeetCode #56 (Merge Intervals) โ€” Needed a Few Tries to Crack It

๐Ÿง  LeetCode #56 (Merge Intervals) โ€” Needed a Few Tries to Crack It

It took me over 2 hours and 3 failed attempts to solve [LeetCode 56: Merge Intervals].

Why?

Because sometimes the problem description and examples just arenโ€™t enough to guide you โ€” and thatโ€™s part of the challenge.

Some tricky cases like:

[[1,4],[2,3]]

[[1,4],[0,4]]

kept failing.

I assumed the intervals were always in sorted order โ€” they werenโ€™t.

Then I peeked at the solution and saw:

sort(intervals.begin(), intervals.end());

That one line changed everything.

I paused, dug into how 2D sorting works in C++, and that helped unlock the logic.


๐Ÿ’ก What I Learned:

โœ… How sorting works for 2D vectors

๐Ÿ—ฃ๏ธ Reading discussions and othersโ€™ code can speed up learning

โœ๏ธ Writing it by hand helped reinforce the logic


๐Ÿ“ธ Sharing my handwritten notes and final accepted solution in C++:

Failed test case

Handwritten notes

Accepted code screenshot


If you're just starting out too โ€” remember: it's okay to take time. The goal is to learn.


๐Ÿท Hashtags

LearnInPublic #Cplusplus #LeetCode #ProblemSolving #BeginnerDev #CodingJourney #DeveloperJourney #DSA #Cpp #Debugging #LeetCodeSolutions #ProblemSolvingSkills

Top comments (0)