DEV Community

Ertugrul
Ertugrul

Posted on • Edited on

πŸ—“ Daily LeetCode Progress – Day 4

Problems Solved:

  • #121 Best Time to Buy and Sell Stock
  • #11 Container With Most Water

This continues my daily series (Day X: Array + Two Pointers patterns). Today I focused on stock profit maximization with a running minimum, and the classic two-pointer shrinking window trick for container problems, implementing both Python and C++ solutions.


πŸ’‘ What I Learned

Today’s focus was on two key greedy/pointer patterns:

  • Tracking minimum value so far to compute maximum profit efficiently.
  • Using two pointers from both ends to maximize area by moving the shorter side inward.
  • Practiced clean implementations in both Python and C++.
  • Re-learned that off-by-one pointer moves (right += 1 instead of right -= 1) cause IndexError bugs.

🧩 #121 β€” Best Time to Buy and Sell Stock

Python (Greedy with min index)

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        min_index = 0
        result = 0
        for i in range(1,len(prices)):
            profit = prices[i] - prices[min_index]
            result = max(result, profit)
            if prices[i] < prices[min_index]:
                min_index = i
        return result
Enter fullscreen mode Exit fullscreen mode

Why it works:
Keep track of the lowest price seen so far. For each day, compute profit if sold today, update the max.

Time: O(n)
Space: O(1)

C++ (Greedy with min index)

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int min_index = 0;
        int result = 0;
        int profit;
        for(int i =1; i <prices.size();i++){
            profit = prices[i] - prices[min_index];
            result = max(result, profit);
            if (prices[i] < prices[min_index]){
                min_index = i;
            }
        }
        return result;
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works:
Same running minimum trick, implemented with integer indices.


🧩 #11 β€” Container With Most Water

Python (Two pointers)

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left = 0
        right = len(height)-1
        best = 0
        while left < right:
            width = right - left
            h = height[left] if height[left] < height[right] else height[right]
            area = width*h
            if area > best:
                best = area
            if height[left] < height[right]:
                left +=1
            else:
                right -=1
        return best
Enter fullscreen mode Exit fullscreen mode

Why it works:
The limiting factor is the shorter line. Shrinking the longer side never increases area, so always move the shorter side inward.

Time: O(n)
Space: O(1)

C++ (Two pointers)

class Solution {
public:
    int maxArea(vector<int>& height) {
        int left = 0, right = height.size() -1 , best = 0,width, h, area;
        while (left < right){
            width = right - left;
            if (height[left] < height[right]){
                h = height[left];
            }else{
                h = height[right];
            }
            area = width *h;
            if (area > best){
                best = area;
            }
            if(height[left] < height[right]){
                left +=1;
            }else{
                right -=1;
            }
        }
        return best;
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works:
Two-pointer shrinking strategy ensures we explore only O(n) pairs instead of O(n^2).


πŸ“Έ Achievements

  • Best Time to Buy and Sell Stock (Python & C++):

besttime python

besttime cpp

  • Container With Most Water (Python & C++):

mostwater python

mostwater cpp


πŸ“¦ Complexity Recap

  • Best Time to Buy and Sell Stock: O(n) time, O(1) space
  • Container With Most Water: O(n) time, O(1) space

πŸ“£ Join the Journey

I’m solving and documenting problems daily in both Python and C++, highlighting patterns and trade-offs. Follow along if you’re into algorithms and performance tuning.

Links

Top comments (0)