updated problem description, test cases and test class to match n-sales

master
Lothar Buchholz 4 years ago
parent c86c1ba120
commit 30543abaf9

@ -1,33 +1,37 @@
# Buy and Sell Twice
# Buy and Sell n-times
## Goal
Given is the price-development of an arbitrary stock.
![stock-price over time](doc/stock-price.JPG)
Suppose you have perfect knowledge and are able to buy and sell the stock **twice**.
Write an algorithm that outputs the maximum profit that can be made.
Suppose you have perfect knowledge and are able to buy and sell the stock **multiple times**.
Write an algorithm that outputs the maximum profit that can be made by exactly n sales.
* The price-development is given for an arbitrary number of days.
* A stock can only be sold on the next day.
* Stock-prices are given as integers
* **The second buy must be made on another date after the first sale**
* **Each buy must be made on another date after the previous sale**
### Input
A blank-separated array of stock-prices.
A blank-separated array of numbers.
The first number is the number of sales allowed.
All following numbers are stock-prices.
Each position symbolises the stock-price of a given day.
### Output
The maximum profit that can be made within that day.
The maximum profit that can be made within that period by `n` sales.
### Constraints
* 4 <= `days with stock-prices` <= 50
* 2 <= `n` <= 10
* 2 * `n` <= `days with stock-prices` <= 50
* 1 <= `stock-price` <= 2,147,483,647
### Example
**Input:**
`12 11 13 9 12 8 14 13 15`
`3 12 11 13 9 12 8 14 13 15`
where `n` is `3`
**Output:**
`10`
`12`

@ -1 +1 @@
rootProject.name = 'buy-and-sell-twice'
rootProject.name = 'buy-and-sell-n-times'

@ -23,8 +23,8 @@ class SolutionTest {
}
@ParameterizedTest
@CsvFileSource( resources = "testdata.csv" )
void main( final String input, final String expected ) throws IOException {
@CsvFileSource( resources = "testdata.csv", numLinesToSkip = 1 )
void main( final String sellCount, final String input, final String expected ) throws IOException {
// keep original streams
InputStream oldIn = System.in;
PrintStream oldOut = System.out;
@ -33,7 +33,7 @@ class SolutionTest {
try {
redirectStreams(
new ByteArrayInputStream( input.getBytes( UTF_8 )),
new ByteArrayInputStream( (sellCount + " " + input).getBytes( UTF_8 )),
new PrintStream( bos, true, UTF_8 ),
new PrintStream( new ByteArrayOutputStream(), true, UTF_8 )
);

@ -1,18 +1,21 @@
12 11 13 9 12 8 14 13 15, 10
310 315 275 295 260 270 290 230 255 250, 55
1 2 1 2, 2
30 29 28 27, 0
0 0 0 0, 0
1 0 0 0, 0
0 1 1 1, 1
226 177 465 365 425 178 328 325 428 165, 538
17 261 266 202 246 220 420 390 120 206, 489
100 100 40 40 40 70 70 70, 30
404 370 48 478 386 157 48 500 443 345, 882
404 370 48 478 386 157 152 500 443 345, 778
404 370 500 48 478 386 157 152 443 345, 721
71622 29778 49278 43266 35321 64004 11569 15358 76325 46324 21209 68079 14505 16821 19481 45622 15044 91459 44742 14731 32402 97619 18531 23351 46294 6443 42565 43976 10390 89339 50414 80745 70068 68925 6460 71966 82808 76650 51168 37177 62789 6277 81398 91852 88771 91089 41410 51305 62464 46015, 171625
68941 71600 57442 66633 95154 35131 42141 99811 63172 48106 8475 98869 38000 53928 19145 40854 12274 62055 98571 75665 5203 17280 97920 92034 78957 87443 77211 86404 19438 32631 89925 10809 77909 41390 96105 78035 78285 21024 35521 13358 67241 17009 33381 59371 55581 46024 97347 42960 29149 31541, 183111
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
sellCount, prices, maxProfit
2, 12 11 13 9 12 8 14 13 15, 10
3, 12 11 13 9 12 8 14 13 15, 12
2, 310 315 275 295 260 270 290 230 255 250, 55
3, 310 315 275 295 260 270 290 230 255 250, 75
2, 310 315 255 275 260 270 285 230 255 250, 55
3, 310 315 255 275 260 270 285 230 255 250, 70
4, 310 315 255 275 260 270 285 230 255 250, 75
2, 310 230 225 230 240 255 275 300 290 295, 80
3, 310 230 225 230 240 255 275 300 290 295, 80
4, 310 230 225 230 240 255 275 300 290 295, 80
2, 1 2 1 2, 2
3, 30 29 28 27 26 25, 0
2, 0 0 0 0, 0
4, 0 0 0 0 0 0 0 0 0 0 0, 0
3, 1 0 0 0 0 0 0 0, 0
4, 0 1 1 1 1 1 1 1 1 1, 1
2, 5 10 15 25 100 200 190 185 180, 195
4, 5 10 15 25 100 200 190 185 180, 195
2, 5 10 15 25 100 200 205 215 220, 215
4, 5 10 15 25 100 200 205 215 220, 215

1 12 11 13 9 12 8 14 13 15 sellCount 10 prices maxProfit
2 310 315 275 295 260 270 290 230 255 250 2 55 12 11 13 9 12 8 14 13 15 10
3 1 2 1 2 3 2 12 11 13 9 12 8 14 13 15 12
4 30 29 28 27 2 0 310 315 275 295 260 270 290 230 255 250 55
5 0 0 0 0 3 0 310 315 275 295 260 270 290 230 255 250 75
6 1 0 0 0 2 0 310 315 255 275 260 270 285 230 255 250 55
7 0 1 1 1 3 1 310 315 255 275 260 270 285 230 255 250 70
8 226 177 465 365 425 178 328 325 428 165 4 538 310 315 255 275 260 270 285 230 255 250 75
9 17 261 266 202 246 220 420 390 120 206 2 489 310 230 225 230 240 255 275 300 290 295 80
10 100 100 40 40 40 70 70 70 3 30 310 230 225 230 240 255 275 300 290 295 80
11 404 370 48 478 386 157 48 500 443 345 4 882 310 230 225 230 240 255 275 300 290 295 80
12 404 370 48 478 386 157 152 500 443 345 2 778 1 2 1 2 2
13 404 370 500 48 478 386 157 152 443 345 3 721 30 29 28 27 26 25 0
14 71622 29778 49278 43266 35321 64004 11569 15358 76325 46324 21209 68079 14505 16821 19481 45622 15044 91459 44742 14731 32402 97619 18531 23351 46294 6443 42565 43976 10390 89339 50414 80745 70068 68925 6460 71966 82808 76650 51168 37177 62789 6277 81398 91852 88771 91089 41410 51305 62464 46015 2 171625 0 0 0 0 0
15 68941 71600 57442 66633 95154 35131 42141 99811 63172 48106 8475 98869 38000 53928 19145 40854 12274 62055 98571 75665 5203 17280 97920 92034 78957 87443 77211 86404 19438 32631 89925 10809 77909 41390 96105 78035 78285 21024 35521 13358 67241 17009 33381 59371 55581 46024 97347 42960 29149 31541 4 183111 0 0 0 0 0 0 0 0 0 0 0 0
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 3 179553 1 0 0 0 0 0 0 0 0
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 4 176493 0 1 1 1 1 1 1 1 1 1 1
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 2 193379 5 10 15 25 100 200 190 185 180 195
19 4 5 10 15 25 100 200 190 185 180 195
20 2 5 10 15 25 100 200 205 215 220 215
21 4 5 10 15 25 100 200 205 215 220 215
Loading…
Cancel
Save