Data Structures and Algorithm

Runtime
java 21.0.12
Files
22 files · 16 KB
Runs
50 runs
Published
StocksSpan.java657 B
public class StocksSpan {    public static void main(String[] args) {        // prices = [7,1,5,3,6,4]        // output = 5        // idx 1 & 6        int[] prices = {7,1,5,3,6,4,8};        int minPrice = Integer.MAX_VALUE;        int maxProfit = 0;​        for (int i = 0 ; i < prices.length ; i++) {            minPrice = Math.min(minPrice, prices[i]);            int profit = prices[i] - minPrice;            maxProfit = Math.max(maxProfit, profit);        }        System.out.println(maxProfit);    }​    loop 0    price = 7;    minPrice = 7;    profit = 0    maxProfite = 0​    loop 1    price 1    minPrice = 1    profit = 0;}​