java implementation of stock buy sell problem

Solutions on MaxInterview for java implementation of stock buy sell problem by the best coders in the world

showing results for - "java implementation of stock buy sell problem"
Jona
25 Sep 2017
1                        
2public int largestProfit(int[] prices) {
3    int profit = 0;
4    int min = Integer.MAX_VALUE;
5    for(int i = 0; i < prices.length; i++) {
6        if (prices[i] > min) {
7            profit = Math.max(profit, prices[i] - min);
8        } else {
9            min = prices[i];
10        }
11    }
12
13    return profit;
14}
15                        
16                    
similar questions