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 ofright -= 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
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;
}
};
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
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;
}
};
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++):
- Container With Most Water (Python & C++):
π¦ 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
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)