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.stream.*;
class Solution {
@@ -9,16 +10,14 @@ class Solution {
.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 );
}
}
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( maxProfit );