Data Structures and Algorithm
- Runtime
- java 21.0.12
- Files
- 22 files · 16 KB
- Runs
- 50 runs
- Published
public class StockSpanMulti { public static void main(String[] args) { //Input: prices = [7,1,5,3,6,4] // Output: 7 int[] prices = {7,6,4,3,1}; int minPrice = Integer.MAX_VALUE; int maxProfit = 0; for (int i = 1 ; i < prices.length ; i++) { // minPrice = Math.min(minPrice, prices[i]); // int profit = prices[i] - minPrice; // maxProfit = Math.max(maxProfit, profit); if (prices[i] > prices[i - 1]) { int diff = prices[i] - prices[i-1]; maxProfit = maxProfit + diff; } } System.out.println(maxProfit); } }