From bbe033c582a9852b4223b661d8598b862ad43553 Mon Sep 17 00:00:00 2001 From: Lothar Buchholz Date: Fri, 3 Apr 2020 23:19:29 +0200 Subject: [PATCH] optimized array solution --- src/main/java/Solution.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/Solution.java b/src/main/java/Solution.java index 5615be4..849ebe9 100644 --- a/src/main/java/Solution.java +++ b/src/main/java/Solution.java @@ -1,20 +1,27 @@ import java.util.*; -import java.io.*; -import java.math.*; -/** - * 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 + int maxProfit = 0; + for ( int index = 0; index < prices.length; index++ ) { + var currentPrice = prices[index]; + + for ( int rem = index + 1; rem < prices.length; rem++ ) { + var futurePrice = prices[rem]; + var profit = futurePrice - currentPrice; + maxProfit = Math.max( profit, maxProfit ); + } + } // Write result with System.out.println() - System.out.println( "value" ); + System.out.println( maxProfit ); } + }