From 95a7c1ffcf8f14696b85c9e08dec5fabc26d7e86 Mon Sep 17 00:00:00 2001 From: Lothar Buchholz Date: Fri, 3 Apr 2020 22:57:23 +0200 Subject: [PATCH] stream solution (not optimized) --- src/main/java/Solution.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/Solution.java b/src/main/java/Solution.java index 5615be4..4cd037d 100644 --- a/src/main/java/Solution.java +++ b/src/main/java/Solution.java @@ -1,20 +1,26 @@ import java.util.*; -import java.io.*; -import java.math.*; +import java.util.stream.*; -/** - * Template code to help you parse the standard input - * according to the problem statement. - **/ class Solution { public static void main( String[] args ) { - Scanner in = new Scanner( System.in ); // read values with in.next...() methods + var prices = new Scanner( System.in ).tokens() + .mapToInt( Integer::valueOf ) + .toArray(); // code your solution here + var maxProfit = IntStream.range( 0, prices.length ) + .map( index -> { + var range = Arrays.copyOfRange( prices, index, prices.length ); + var max = IntStream.of( range ).max().orElse( 0 ); + // difference between current price and future maximum + return max - prices[index]; + }) + .max().orElse( 0 ); // Write result with System.out.println() - System.out.println( "value" ); + System.out.println( maxProfit ); } + }