forked from code-clash/buy-and-sell-once
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
bbe033c582 |
@@ -1,5 +1,4 @@
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
class Solution {
|
||||
|
||||
@@ -10,14 +9,16 @@ class Solution {
|
||||
.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 );
|
||||
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( maxProfit );
|
||||
|
Reference in New Issue
Block a user