forked from code-clash/buy-and-sell-once
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
95a7c1ffcf |
@@ -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 );
|
||||||
|
Reference in New Issue
Block a user