You are given an array of integers 'prices' of size 'n', where ‘prices[i]’ is the price of a given stock on an ‘i’-th day. You want to maximize the profit by choosing a single day to buy one stock and a different day to sell that stock.Please note that you can’t sell a stock before you buy one.Detailed explanation ( Input/output format, Notes, Images )Constraints :1 <= 'n' <= 10 ^ 51 <= ‘prices[i]’<= 10 ^ 9Time Limit: 1 secSample Input 1:67 1 5 4 3 6Sample Output 1 :5Explanation Of Sample Input 1:Purchase stock on day two, where the price is one, and sell it on day six, where the price is 6. Profit = 6 - 1 = 5.Sample Input 2:55 4 3 2 1Sample Output 2 :0
Question
You are given an array of integers 'prices' of size 'n', where ‘prices[i]’ is the price of a given stock on an ‘i’-th day. You want to maximize the profit by choosing a single day to buy one stock and a different day to sell that stock.Please note that you can’t sell a stock before you buy one.Detailed explanation ( Input/output format, Notes, Images )Constraints :1 <= 'n' <= 10 ^ 51 <= ‘prices[i]’<= 10 ^ 9Time Limit: 1 secSample Input 1:67 1 5 4 3 6Sample Output 1 :5Explanation Of Sample Input 1:Purchase stock on day two, where the price is one, and sell it on day six, where the price is 6. Profit = 6 - 1 = 5.Sample Input 2:55 4 3 2 1Sample Output 2 :0
Solution
To solve this problem, we can use a simple approach of iterating through the array and keeping track of the minimum price and the maximum profit we can get. Here are the steps:
-
Initialize two variables,
minPriceandmaxProfit. SetminPriceto the maximum possible value of an integer andmaxProfitto 0. -
Iterate through the array. For each price:
a. If the price is less than
minPrice, updateminPricewith the current price.b. Calculate the potential profit by subtracting
minPricefrom the current price. If the potential profit is greater thanmaxProfit, updatemaxProfitwith the potential profit. -
After iterating through all prices,
maxProfitwill hold the maximum profit we can get.
Here is a Python code snippet that implements the above steps:
def maxProfit(prices):
minPrice = float('inf')
maxProfit = 0
for price in prices:
minPrice = min(minPrice, price)
profit = price - minPrice
maxProfit = max(maxProfit, profit)
return maxProfit
In the first sample input, the function will return 5, which is the maximum profit we can get by buying the stock on day 2 and selling it on day 6. In the second sample input, the function will return 0, because the prices are decreasing every day, so there is no opportunity to make a profit.
Similar Questions
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.Find and return the maximum profit you can achieve.
Maximum ProfitSay you have an array, A, for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most 2 transactions.Return the maximum possible profit.Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).Problem Constraints1 <= length(A) <= 7e51 <= A[i] <= 1e7Sample input57 2 4 8 7Sample output6ExplanationInput: 57 2 4 8 7The first line contains the number of elements in the array.And, the second line contains the element of array.Output:6
Return the buy and sell prices to generate the most profit from a list of daily stock prices (integers for simplicity). The single buy/sell profit must be maximized. If you can't make any money, strive to cut your losses as much as possible.
Finding the Maximum Stock PriceYou are an analyst tracking the stock prices of a company over the past week (7 days). Write a program that reads the stock prices into an array and finds the maximum stock price during this period.Constraints:NAExample:Sample Input:1001021059998110107Sample Output:110
Arun is a stock trader who wants to maximize his profit by buying and selling stocks at the right time. He has two lists of stock prices for two different companies. Arun wants to merge these lists and find the maximum profit he can make by buying and selling stocks from the merged list.Write a program to help Arun find the maximum profit he can make from the merged list of stock prices.ExampleInput:25 1028 15Output:10Explanation: The maximum profit can be obtained by buying at price 5 and selling at price 15, resulting in a profit of 15 - 5 = 10.Company Tags: AdobeInput format :The first line of input consists of an integer N, representing the size of the first dataset.The second line consists of N space-separated integers, representing the daily stock prices in the first dataset.The third line consists of an integer M, representing the size of the second dataset.The fourth line consists of M space-separated integers, representing the daily stock prices in the second dataset.Output format :The output prints a single integer, representing the maximum profit that can be made by buying and selling the stock.Refer to the sample output for formatting specifications.Code constraints :1 ≤ N, M ≤ 101 ≤ stock prices ≤ 500Sample test cases :Input 1 :25 1028 15Output 1 :10Input 2 :37 3 642 9 5 4Output 2 :7
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.