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.*;
|
||||||
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 );
|
||||||
|
Reference in New Issue
Block a user