-4

I study computer sciene and I'm currently practising backtracking as I#m very bad at it. I found that exercise online:

You are selling apples. This is a current price tables for your apples:

Count 1 2 3 4 5 6 7 8 Price 1 5 8 9 10 17 17 20

So if you're selling 8 apples at once, you'll get 20$. If you're selling 6 and then 2 instead, you'll get 22$. Try to find the maximum profit. Here's the method, you should use:

public static long sellApples(int count, long[] prices) {

}

Now I thought about that like 5 hours, but I can't come to a good solution. Anyone up for a little challenge?

waayne
  • 49
  • 6
  • 1
    Have you tried anything beyond that method stub yet? – MTCoster Nov 17 '18 at 11:25
  • you need to split count into pieces, like `8` into `3, 5`. Then you need to call this method with both of these numbers as parameters and sum the result. – László Stahorszki Nov 17 '18 at 11:28
  • 1
    @MTCoster No, I try to stick to the exercise there. – waayne Nov 17 '18 at 11:30
  • @LászlóStahorszki Yeah, that's what i thought about too, but I don't know how to implement that properly... – waayne Nov 17 '18 at 11:31
  • What I meant is, have you written any additional code which you haven’t posted here? – MTCoster Nov 17 '18 at 11:31
  • @MTCoster public static long verkaufenNaive(int stoff, long[] preise) { if(stoff <= 0) return 0L; else { if() } } – waayne Nov 17 '18 at 11:37
  • 2
    @waayne The point I’m trying to make is that *we are not here to write your code for you*. SO is a place to get help with code you’ve *already written* and can’t get working. – MTCoster Nov 17 '18 at 11:38

2 Answers2

0

I'm not an expert on this subject, so take what I say with a grain of salt.

In the method, you need to split count into pieces, like 8 => 3, 5. You need to call this method again, with both of these values as count. That is the more obvious part.

The tricky part is that you need to admit that splitting count into 2 parts is enough. I mean that you don't need to try calling the method with 1,2,5 values, because if you just call it with 3,5, then the next iteration can split 3 into 1,2.

A bit more obvious fact is that if you tried 3,5 then you don't need to try 5,3.

So if your input is 8, then you try 8, 1,7, 2,6, 3,5, 4,4 and compare the results, choosing the biggest one. From the method stub you provided, I assume that you don't need to return how you got to that result, just the max result itself

László Stahorszki
  • 1,102
  • 7
  • 23
  • But what if the best price / apple is when selling them in pairs, then the optimal combination would be 2-2-2-2? – Joakim Danielson Nov 17 '18 at 11:44
  • yes. Then the program would split the original input `8` into `4,4` in the first iteration, and the second iteration would split `4` into `2,2` both times. The program would try splitting 2 into `1,1`, but that would be smaller than the original `2`, which will be the return value – László Stahorszki Nov 17 '18 at 11:47
  • I realize that this is probably an O(2^n) algorithm (I haven't checked). But it's better then no algorithm I guess – László Stahorszki Nov 17 '18 at 11:50
-1

hope this will help you.

    public static long sellApples(int count, long[] prices) {
    long maxKnownValue = 0L;
    if(count==1){
        return prices[0];
    }
    for(int i=1;i<=prices.length && i<=count;i++){
        long valueCandidate = sellApples(count-i, prices)+prices[i-1];
        if(valueCandidate>maxKnownValue){
            maxKnownValue=valueCandidate;
        }
    }
    return maxKnownValue;
}
  • I've did _some_ testing here. So pls. test and check if there any errors. – dlavochkin Nov 17 '18 at 12:26
  • 2 reasons why I down voted: it's wrong, because it doesn't check if a given count is better then splitting it, which means that the result is just prices[0]×count. The second reason is that you're not meant to solve other ppls homework, just help them if they get stuck – László Stahorszki Nov 17 '18 at 17:34
  • Do you think that the result is just prices[0]×count? Read the code. Carefully. Again. – dlavochkin Nov 18 '18 at 01:23
  • @dlavochkin You definitely have the best bio on Stack Overflow. – skomisa Nov 21 '18 at 05:31