From 6bc6f337307b68a1c432691e24c1b0a2178d1e44 Mon Sep 17 00:00:00 2001 From: rohweini Date: Wed, 8 Apr 2020 07:24:55 +0200 Subject: [PATCH] buy and sell once mysolution --- src/main/java/Solution.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/Solution.java b/src/main/java/Solution.java index 5615be4..36bea2b 100644 --- a/src/main/java/Solution.java +++ b/src/main/java/Solution.java @@ -11,10 +11,22 @@ class Solution { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); // read values with in.next...() methods - + String[] stockPrices =in.nextLine().split(" "); + // code your solution here - + List allProfits = new ArrayList(); + int currentMin = Integer.parseInt(stockPrices[0]); + + for (int i= 0; i< stockPrices.length-1 ; i++) { + if(currentMin < Integer.parseInt(stockPrices[i+1])) { + allProfits.add(Integer.parseInt(stockPrices[i+1]) - currentMin); + } else { + currentMin = Integer.parseInt(stockPrices[i+1]); + } + } + + int value = allProfits.stream().mapToInt(m -> m).max().orElse(0); // Write result with System.out.println() - System.out.println( "value" ); + System.out.println( value); } }