You are given two 0-indexed integer arrays prices
and profits
, representing the price and profit of each stock. You are also given an integer money
, which represents your initial capital.
For every stock, you can buy at most one share. After buying any number of shares of stocks, you can sell them after one day and gain profits[i] - prices[i]
for each share of the stock i
that you bought.
Return the maximum profit you can have after buying and selling the stocks.
Note: There is no limit on the number of stocks you can buy.
Example 1:
Input: prices = [10,15,12,18,20], profits = [25,22,15,24,30], money = 100 Output: 105 Explanation: Buy stock 0, spend 10, and gain 25 - 10 = 15. Buy stock 2, spend 12, and gain 15 - 12 = 3. Buy stock 3, spend 18, and gain 24 - 18 = 6. The total profit is 15 + 3 + 6 = 24. After buying and selling these stocks, your money is 100 - 10 - 12 - 18 + 24 = 84. Buy stock 1, spend 15, and gain 22 - 15 = 7. Buy stock 4, spend 20, and gain 30 - 20 = 10. The total profit is 24 + 7 + 10 = 41. After buying and selling these stocks, your money is 84 - 15 - 20 + 41 = 90. The maximum profit you can have is 15 + 3 + 6 + 7 + 10 = 41. So your final money is 100 + 41 = 141.
Example 2:
Input: prices = [10,15,12,18,20], profits = [15,22,15,24,30], money = 100 Output: 177 Explanation: Buy stock 0, spend 10, and gain 15 - 10 = 5. Buy stock 1, spend 15, and gain 22 - 15 = 7. Buy stock 2, spend 12, and gain 15 - 12 = 3. Buy stock 3, spend 18, and gain 24 - 18 = 6. Buy stock 4, spend 20, and gain 30 - 20 = 10. The total profit is 5 + 7 + 3 + 6 + 10 = 31. After buying and selling these stocks, your money is 100 - 10 - 15 - 12 - 18 - 20 + 31 = 56. Buy stock 1, spend 15, and gain 22 - 15 = 7. Buy stock 3, spend 18, and gain 24 - 18 = 6. Buy stock 4, spend 20, and gain 30 - 20 = 10. The total profit is 31 + 7 + 6 + 10 = 54. After buying and selling these stocks, your money is 56 - 15 - 18 - 20 + 23 = 26. Buy stock 3, spend 18, and gain 24 - 18 = 6. The total profit is 54 + 6 = 60. After buying and selling these stocks, your money is 26 - 18 + 6 = 14. Buy stock 3, spend 14, and gain 24 - 18 = 6. The total profit is 60 + 6 = 66. So your final money is 100 + 66 = 166.
Constraints:
1 <= prices.length == profits.length <= 105
1 <= prices[i], profits[i] <= 105
0 <= money <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to maximizing stock trading profit involves considering every single possible buy and sell combination. We explore each pair of days to see if buying on one day and selling on another yields a profit. This ensures we find the absolute best, even if inefficiently.
Here's how the algorithm would work step-by-step:
def max_profit_brute_force(stock_prices):
max_profit_so_far = 0
number_of_days = len(stock_prices)
for buy_day in range(number_of_days):
# Consider each day as a potential buying day
for sell_day in range(buy_day + 1, number_of_days):
# Try selling on every day after the buy day
profit = stock_prices[sell_day] - stock_prices[buy_day]
# Calculate profit for this buy-sell combination
max_profit_so_far = max(max_profit_so_far, profit)
# Update the maximum profit found so far
return max_profit_so_far
To maximize profit, we aim to buy low and sell high. Instead of checking all possible buy/sell combinations, we use a single pass to track the lowest buying price and sell whenever we see a higher price, accumulating profit along the way.
Here's how the algorithm would work step-by-step:
def max_profit_from_stocks(stock_prices):
max_profit = 0
potential_buying_price = stock_prices[0]
for current_price in stock_prices:
# If current price is higher, sell and add profit
if current_price > potential_buying_price:
max_profit += current_price - potential_buying_price
potential_buying_price = current_price
# Update the buying price if a lower price is found
elif current_price < potential_buying_price:
potential_buying_price = current_price
return max_profit
Case | How to Handle |
---|---|
Null or empty input array | Return 0 as no profit can be made without stock prices. |
Input array with only one element | Return 0 as a single day cannot result in a buy and sell transaction. |
Input array with monotonically decreasing prices (always going down) | Return 0 as there's no opportunity to buy low and sell high. |
Input array with monotonically increasing prices (always going up) | Calculate profit by buying on day 0 and selling on the last day, or accumulate profit from each increasing day. |
Input array with all identical values | Return 0 as there's no price difference to make a profit. |
Integer overflow when accumulating profit with large price differences and many transactions | Use a data type that can accommodate larger values (e.g., long) for profit accumulation to avoid overflow. |
Input array contains very large price values (close to maximum integer) | The algorithm should still work correctly as long as intermediate calculations don't overflow, address with larger data types if needed. |
Alternating high and low prices creating many small profits | The greedy approach of buying low and selling high on each increasing day handles this efficiently. |