1 Commits

Author SHA1 Message Date
95a7c1ffcf stream solution (not optimized) 2020-04-04 19:04:45 +02:00

View File

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