-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.java
More file actions
35 lines (31 loc) · 1.43 KB
/
Copy pathDay6.java
File metadata and controls
35 lines (31 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Stock Buy and Sell – Max one Transaction Allowed
Given an array prices[] of length n, representing the prices of the stocks on different days. The task is to find the maximum profit possible by buying and selling the stocks on different days when at most one transaction is allowed. Here one transaction means 1 buy + 1 Sell. If it is not possible to make a profit then return 0.
Note: Stock must be bought before being sold.
Examples:
Input: prices[] = [7, 10, 1, 3, 6, 9, 2]
Output: 8
Explanation: You can buy the stock on day 2 at price = 1 and sell it on day 5 at price = 9. Hence, the profit is 8.
Input: prices[] = [7, 6, 4, 3, 1]
Output: 0
Explanation: Here the prices are in decreasing order, hence if we buy any day then we cannot sell it at a greater price. Hence, the answer is 0.
Input: prices[] = [1, 3, 6, 9, 11]
Output: 10
Explanation: Since the array is sorted in increasing order, we can make maximum profit by buying at price[0] and selling at price[n-1]. */
/*// User function Template for Java
class Solution {
public int maximumProfit(int prices[]) {
// Code here
int minPrice =prices[0];
int maxProfit=0;
for(int i=1;i<prices.length;i++){
if(prices[i]<minPrice){
minPrice=prices[i];
}
int profit =prices[i]-minPrice;
if(profit>maxProfit){
maxProfit=profit;
}
}
return maxProfit;
}
}*/