Day 6: Cracking My First Hard Problem & Leveling Up with Stacks, Two Pointers & Sliding Window πŸš€

Tennis Kumar CTennis Kumar C
3 min read

πŸ“Œ Today’s Progress

  • First time facing a hard problemβ€”and honestly, after solving it, the solution felt much simpler than expected.

  • Key realization: Understanding the question before attempting to solve it makes a huge difference.

  • Covered Stack, Two Pointers, and Sliding Window problems today.

  • Took quite a few breaks, but overall, it was a pretty productive day!


πŸ“ Problems Solved Today

Stack

1️⃣ Daily Temperatures (Medium)
πŸ’‘ Concept: Monotonic Stack
πŸ” Approach:

  • Used a decreasing stack to track temperatures and find how many days until a warmer temperature.

  • If the current temperature is higher than the top of the stack, pop elements and calculate the difference.
    ⏳ Time Complexity: O(n)

2️⃣ Car Fleet (Medium)
πŸ’‘ Concept: Stack + Sorting
πŸ” Approach:

  • Sorted cars by position and iterated from the back, maintaining a stack to track the fleets.

  • If a car reaches the destination after another, it forms a new fleet.
    ⏳ Time Complexity: O(n log n)

3️⃣ Largest Rectangle in Histogram (Hard)
πŸ’‘ Concept: Monotonic Stack
πŸ” Approach:

  • Used a stack to track bar heights, calculating max areas whenever we encountered a smaller bar.

  • Popped elements from the stack to determine the widest possible rectangle.
    ⏳ Time Complexity: O(n)


Two Pointers

4️⃣ Valid Palindrome (Easy)
πŸ’‘ Concept: Two Pointers
πŸ” Approach:

  • Filtered out non-alphanumeric characters and used two pointers to compare characters from both ends.
    ⏳ Time Complexity: O(n)

5️⃣ Two Sum II - Sorted Array (Medium)
πŸ’‘ Concept: Two Pointers
πŸ” Approach:

  • Used two pointers, starting at both ends, moving them closer based on sum comparison with the target.
    ⏳ Time Complexity: O(n)

6️⃣ 3Sum (Medium)
πŸ’‘ Concept: Sorting + Two Pointers
πŸ” Approach:

  • Sorted the array and used a fixed pointer with two moving pointers to find unique triplets summing to zero.
    ⏳ Time Complexity: O(n²)

7️⃣ Container With Most Water (Medium)
πŸ’‘ Concept: Two Pointers
πŸ” Approach:

  • Used two pointers moving inward to maximize water storage while maintaining the widest possible width.
    ⏳ Time Complexity: O(n)

8️⃣ Trapping Rain Water (Hard)
πŸ’‘ Concept: Two Pointers
πŸ” Approach:

  • Maintained two pointers tracking the max left and right boundaries to calculate trapped water efficiently.
    ⏳ Time Complexity: O(n)

Sliding Window

9️⃣ Best Time to Buy and Sell Stock (Easy)
πŸ’‘ Concept: Sliding Window (Optimized as a One-Pass Solution)
πŸ” Approach:

  • Used a single pass to track the minimum price so far and update the maximum profit dynamically.

  • The key was to keep an eye on the lowest buying price and sell whenever we found a higher price that increased profit.
    ⏳ Time Complexity: O(n)

πŸ”Ÿ Longest Substring Without Repeating Characters (Medium)
πŸ’‘ Concept: Sliding Window + HashSet
πŸ” Approach:

  • Used a moving window and a HashSet to track unique characters.

  • If a duplicate was found, moved the left pointer until uniqueness was restored.
    ⏳ Time Complexity: O(n)


πŸš€ Takeaways

πŸ”Ή Tackling a hard problem gave a huge confidence boost. It was intimidating at first, but once broken down, the logic clicked.
πŸ”Ή Recognizing problem patterns (stacks, two pointers, sliding window) makes solving easier.
πŸ”Ή Breaking down the problem before jumping into code is key. Saves time and avoids confusion.

Even with a few breaks, today was a solid grind! On to the next one. πŸ”₯

0
Subscribe to my newsletter

Read articles from Tennis Kumar C directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tennis Kumar C
Tennis Kumar C