stream solution for two sales

master
Lothar Buchholz 4 years ago
parent c86c1ba120
commit 2cb56d6ee4

@ -1,20 +1,36 @@
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.stream.IntStream;
/**
* Template code to help you parse the standard input
* according to the problem statement.
**/
class Solution {
public static void main( String[] args ) {
Scanner in = new Scanner( System.in );
// read values with in.next...() methods
var prices = new Scanner( System.in ).tokens()
.mapToInt( Integer::valueOf )
.toArray();
// code your solution here
var maxSum = IntStream.range( 2, prices.length - 1 )
.map( sellIndex -> {
var firstRange = Arrays.copyOfRange( prices, 0, sellIndex );
var secondRange = Arrays.copyOfRange( prices, sellIndex - 1, prices.length );
return maxProfit( firstRange ) + maxProfit( secondRange );
})
.max().orElse( 0 );
// Write result with System.out.println()
System.out.println( "value" );
System.out.println( maxSum );
}
private static int maxProfit( int[] prices ){
return 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 );
}
}

@ -16,3 +16,5 @@
50475 99931 71645 4336 47287 16439 28493 22806 45724 60734 55445 41931 90369 7416 49331 87500 56317 76342 64624 22835 63579 20350 46476 73361 30444 21531 12824 59458 38753 71993 58684 8816 86301 22702 23316 5111 44782 69841 42297 98631 23986 78318 25994 77107 2844 25915 81602 80089 45663 20118, 179553
31415 57223 42652 20693 88629 96495 39321 61282 16147 73229 18819 91069 8584 40336 18775 16328 96503 62165 96146 95743 94957 8990 41718 73242 42438 80072 55036 19633 82085 86487 29457 91539 32383 72521 84821 97564 85805 69144 65539 62642 95499 51508 18834 76619 84593 10853 34456 25352 70698 89234, 176493
7580 99445 81284 81448 72881 98705 29410 11793 3574 43699 11461 41322 7330 22885 27159 55953 89069 76241 18863 4448 29988 87771 70582 16636 56644 71363 64399 69087 61412 81993 46698 87018 4856 4805 94973 57420 569 95761 1778 90433 99965 5663 48030 40092 71133 21890 20249 64628 29807 90259, 193379
5 10 15 25 100 200 190 185 180, 195
5 10 15 25 100 200 205 215 220, 215

1 12 11 13 9 12 8 14 13 15 10
16 50475 99931 71645 4336 47287 16439 28493 22806 45724 60734 55445 41931 90369 7416 49331 87500 56317 76342 64624 22835 63579 20350 46476 73361 30444 21531 12824 59458 38753 71993 58684 8816 86301 22702 23316 5111 44782 69841 42297 98631 23986 78318 25994 77107 2844 25915 81602 80089 45663 20118 179553
17 31415 57223 42652 20693 88629 96495 39321 61282 16147 73229 18819 91069 8584 40336 18775 16328 96503 62165 96146 95743 94957 8990 41718 73242 42438 80072 55036 19633 82085 86487 29457 91539 32383 72521 84821 97564 85805 69144 65539 62642 95499 51508 18834 76619 84593 10853 34456 25352 70698 89234 176493
18 7580 99445 81284 81448 72881 98705 29410 11793 3574 43699 11461 41322 7330 22885 27159 55953 89069 76241 18863 4448 29988 87771 70582 16636 56644 71363 64399 69087 61412 81993 46698 87018 4856 4805 94973 57420 569 95761 1778 90433 99965 5663 48030 40092 71133 21890 20249 64628 29807 90259 193379
19 5 10 15 25 100 200 190 185 180 195
20 5 10 15 25 100 200 205 215 220 215
Loading…
Cancel
Save