Problem 60296. Maximizing Stock Profit
As a stock exchange broker focusing on a single transaction, your goal is to maximize profit by buying a stock at a low price and selling them later at a higher price within a specific timeframe.
Your function needs to analyze the price fluctuations of the stock over time to identify the most profitable opportunity. It should calculate the maximum potential profit achievable within these fluctuations by finding the highest price to sell the stock after buying it at the lowest possible price. If there's no opportunity to make a profit, such as when the stock price consistently decreases or remains the same, the function should return zero, indicating that there's no viable opportunity for a profitable transaction.
For example, given stock prices [6, 2, 1, 2, 3, 2, 3, 4, 5, 4], the function must return 4. This is because the maximum profit is obtained by buying at 1 and selling at 5, resulting in a profit of 5-1=4.
Solution Stats
Solution Comments
Show commentsProblem Recent Solvers8
Suggested Problems
-
1196 Solvers
-
The Hitchhiker's Guide to MATLAB
3296 Solvers
-
Project Euler: Problem 4, Palindromic numbers
1041 Solvers
-
I've got the power! (Inspired by Project Euler problem 29)
120 Solvers
-
Where the Four Corners Am I? (Vectorized)
93 Solvers
More from this Author53
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!