| 1 | import java.util.*; |
| 2 | |
| 3 | public class ArraysCC { |
| 4 | |
| 5 | public static int buyAndSellStocks(int prices[]) { |
| 6 | int buyPrice = Integer.MAX_VALUE; |
| 7 | int maxProfit = 0; |
| 8 | |
| 9 | for(int i=0; i<prices.length; i++) { |
| 10 | if(buyPrice < prices[i]) { //profit |
| 11 | int profit = prices[i] - buyPrice; // today's profit |
| 12 | maxProfit = Math.max(maxProfit, profit); |
| 13 | } else { |
| 14 | buyPrice = prices[i]; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return maxProfit; |
| 19 | } |
| 20 | public static void main(String args[]) { |
| 21 | int prices[] = {7, 1, 5, 3, 6, 4}; //O(n) |
| 22 | System.out.println(buyAndSellStocks(prices)); |
| 23 | } |
| 24 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…