LeetCode “Sum” Trinity & The Return to the Court (Day 5)
Photo by James T on Unsplash

Day 5 was basically a tactical retreat from the desk.

Free to read link here

Things have been kind rough. I mean we were going smooth when I made this routine and all of a sudden the work spiked. And usually, we do not have so much work in the last month of the year, but it’s been so different this year.

Anyway, after a hectic week of 10 hour work shifts, I was afraid and could feel the burnout creeping in.

As I had said, the “War Time” roadmap requires grinding, but it also requires keeping the machine (my body) functional.

So, I traded an hour of study time for an hour of badminton.

Some people were genuinely happy to see me back on the court :)

It felt good, trust me.

I came back, showered.

And remembered

the streak must go on.

Knocked out the “Sum” trilogy.

These are classics for a reason! They perfectly illustrate the trade-off between Space and Time.

1. Two Sum (The Classic)

Constraint: Array is unsorted.

Logic: Use a Hash Map to store value -> index.

  • As you iterate, check if target - current_value exists in the map.
  • Time: O(N) | Space: O(N)

2. Two Sum II (Input Array Is Sorted)

Constraint: Array is sorted. 1-indexed.

Logic: Since it is sorted, we don’t need a Hash Map. We use Two Pointers.

  • Put one pointer at the start (left), one at the end (right).
  • If sum > target: Move right down (decrease sum).
  • If sum < target: Move left up (increase sum).
  • Time: O(N) | Space: O(1). (This is the optimization).

3. 3Sum (The Big Boss xD)

Constraint: Find unique triplets that sum to 0.

Logic: This is just Two Sum II wrapped in a loop.

  1. Sort the array.
  2. Iterate with index i.
  3. For each i, run the Two Pointer logic on the remaining array to find pairs that sum to -nums[i].
  4. The Catch: Duplicates.
  • If nums[i] == nums[i-1], skip it.
  • Inside the two-pointer loop, if you find a match, move both pointers past any duplicate values to avoid printing the same triplet twice.

The Life Log

  • Activity: 1 hour of intense Badminton.
  • Mood: Reset.
  • Diet: Better. Post-game hunger usually leads to better meals.