1 Commits

Author SHA1 Message Date
bbe033c582 optimized array solution 2020-04-03 23:21:58 +02:00

View File

@@ -1,5 +1,4 @@
import java.util.*; import java.util.*;
import java.util.stream.*;
class Solution { class Solution {
@@ -10,14 +9,16 @@ class Solution {
.toArray(); .toArray();
// code your solution here // code your solution here
var maxProfit = IntStream.range( 0, prices.length ) int maxProfit = 0;
.map( index -> { for ( int index = 0; index < prices.length; index++ ) {
var range = Arrays.copyOfRange( prices, index, prices.length ); var currentPrice = prices[index];
var max = IntStream.of( range ).max().orElse( 0 );
// difference between current price and future maximum for ( int rem = index + 1; rem < prices.length; rem++ ) {
return max - prices[index]; var futurePrice = prices[rem];
}) var profit = futurePrice - currentPrice;
.max().orElse( 0 ); maxProfit = Math.max( profit, maxProfit );
}
}
// Write result with System.out.println() // Write result with System.out.println()
System.out.println( maxProfit ); System.out.println( maxProfit );