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


π 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. π₯
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
