Problem Number: 121 Difficulty: Easy Category: Array, Dynamic Programming, Greedy LeetCode Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
You are given an array prices where prices[i] is the price of a given stock on the i^th day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 10^50 <= prices[i] <= 10^4
I used a Greedy approach with single pass tracking. The key insight is to keep track of the minimum price seen so far and calculate the potential profit at each day.
Algorithm:
- Initialize minimum price to infinity and maximum profit to 0
- Iterate through each price in the array
- Update minimum price if current price is lower
- Calculate potential profit: current price - minimum price
- Update maximum profit if current profit is higher
- Return maximum profit found
The solution uses a greedy approach with single pass tracking. See the implementation in the solution file.
Key Points:
- Tracks minimum price seen so far
- Calculates potential profit at each day
- Updates maximum profit when better opportunity found
- Single pass through the array
- Handles case where no profit is possible
Time Complexity: O(n)
- Single pass through the array
- Each iteration performs constant time operations
- Total: O(n)
Space Complexity: O(1)
- Uses only a constant amount of extra space
- No additional data structures needed
-
Greedy Approach: We can find the maximum profit by tracking the minimum price and calculating potential profits.
-
Single Pass: We only need to traverse the array once, keeping track of the minimum price seen so far.
-
Buy Before Sell: The constraint that we must buy before selling is naturally handled by tracking minimum price.
-
No Profit Case: If prices are monotonically decreasing, the maximum profit will be 0.
-
Optimal Substructure: The solution for the entire array depends on the minimum price seen so far.
-
Efficient Tracking: We don't need to store all prices, just the minimum and maximum profit.
-
Brute Force: Initially might try all possible buy-sell pairs, leading to O(n²) complexity.
-
Wrong Order: Not understanding that we must buy before selling.
-
Complex Logic: Overcomplicating the solution with unnecessary data structures.
-
Missing Edge Cases: Not handling cases where no profit is possible.
- Best Time to Buy and Sell Stock II (Problem 122): Multiple transactions allowed
- Best Time to Buy and Sell Stock III (Problem 123): At most two transactions
- Best Time to Buy and Sell Stock IV (Problem 188): At most k transactions
- Best Time to Buy and Sell Stock with Cooldown (Problem 309): With cooldown period
- Dynamic Programming: Use DP array to track maximum profit - O(n) time, O(n) space
- Two Pointers: Use two pointers to track buy and sell days - O(n) time
- Kadane's Algorithm: Adapt Kadane's algorithm for this problem - O(n) time
- Brute Force: Checking all possible buy-sell pairs instead of using greedy approach.
- Wrong Order: Not understanding the buy-before-sell constraint.
- Complex Implementation: Using unnecessary data structures or complex logic.
- Edge Cases: Not handling cases where no profit is possible.
- Inefficient Tracking: Not using single pass approach.
Note: This is a classic greedy problem that demonstrates efficient single-pass profit maximization.