MCPcopy Create free account
hub / github.com/apna-college/Alpha / ArraysCC

Class ArraysCC

1_Arrays/ArraysCC.java:3–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.*;
2
3public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…