MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / maxProfit

Method maxProfit

BestTimetoBuyandSellStock.java:3–20  ·  view source on GitHub ↗
(int[] prices)

Source from the content-addressed store, hash-verified

1class Solution
2{
3 public int maxProfit(int[] prices)
4 {
5 if(prices.length<=1) return 0;
6 int min=prices[0];
7 int profit=0;
8 for(int i=1;i<prices.length;i++)
9 {
10 if(prices[i]<min)
11 {
12 min=prices[i];
13 }
14 else
15 {
16 if(prices[i]-min>profit) profit=prices[i]-min;
17 }
18 }
19 return profit;
20 }
21}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected