Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
Harshad is a newbie to trading, his boss gave him a long list of stock prices prices
where prices[i]
is the price of a stock on the i
th day.
Harshad needs to maximize the profit by choosing a single day to purchase one stock and choose another day in the future to sell that stock.
Help Harshad by designing a function that will return the maximum possible profit that can be achieved by trading stocks as explained above, if there is no profit then return 0.
Examples:
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.
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
max_profit
prices
Complete the below code in the right side coding panel
def max_profit(prices: list) -> int:
# your code goes here
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
No hints are availble for this assesment
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...