There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
fred rosenberger wrote:Forget about java and recursion.
The first thing you need to do is explain your algorithm for loading the ship. I don't understand why you selected "50 40 8". Why isn't it "7 50 40 97" or "7 2 3 8 13 24 40".
In other words, you need to first explain exactly how you select the cargo containers in English.
benjamin parker wrote:any help to guide me? i am trying to find the closest value or equal to my target value.
Henry Wong wrote:
benjamin parker wrote:any help to guide me? i am trying to find the closest value or equal to my target value.
To be blunt... you never answered Fred question. Fred asked how does your algorithm works  and not for more examples (so that presumable, we can figure it out for you), or for an explanation of the goal, which has little to do with the algorithm.
Simply, if you can't explain the algorithm to us, how are you going to code the algorithm into the computer?
Henry
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap wrote:If the weight is the only constraint this seems like a "straightforward" version of a knapsack problem. The obvious way to come to a possible solution recursively would be to implement a recursive backtracking algorithm. The Wikipedia articles I linked to would be a good place to start looking for information.
benjamin parker wrote:
Henry Wong wrote:
benjamin parker wrote:any help to guide me? i am trying to find the closest value or equal to my target value.
To be blunt... you never answered Fred question. Fred asked how does your algorithm works  and not for more examples (so that presumable, we can figure it out for you), or for an explanation of the goal, which has little to do with the algorithm.
Simply, if you can't explain the algorithm to us, how are you going to code the algorithm into the computer?
Henry
The goal of this problem is to get the closest or equal to the target value and not arranging any of the inputs
1) for every integer, carry out recursion for 2 cases. One is taking into account of the integer and and second is not taking into the account of the integer. [when recursion ends, i will have 2^N cases where N refers to the number of integer inputs ]
2) when current index is greater than array size & sum<taraget =>get the difference between the target value and the computed sum and stored as bestSum
3) compare with all the other test cases, the one with the smallest bestSum is reflected as the output
Paul Clapham wrote:You need to be able to evaluate some value which tells you how good your choice was, for any particular choice of entries. In your example it might be the sum of the numbers you chose, for example. Then you need to evaluate that value for each possible choice and identify the choice with the largest value. Or the smallest value, if your metric works that way.
The recursion works like this: Pick one entry and then find the best combination of the remaining entries. Then pick another entry and find the best combination of the remaining entries. And so on. You'll have values for each of those choices, so you just choose the largest of the values. The recursion terminates when there's only one entry to pick.
benjamin parker wrote:i would like to bruteforce everything and store them inside the arraylist.
benjamin parker wrote:i would like to bruteforce everything and store them inside the arraylist. Once it's done i i will call another function to get the best case in the arraylist. can't seem to do that, is there something wrong with my basecase? also the main idea i am unsure is the backtracking part, how do i make it go back one step and try with cases when the integer is not taken into account
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
Paul Clapham wrote:
benjamin parker wrote:i would like to bruteforce everything and store them inside the arraylist.
What's "everything" and what's "them" and what arraylist is that?
If you're given for example 8 numbers then there are 256 ways to choose a subset of those 8 numbers. Does "them" refer to the 256, or what? Basically you're going to have to produce a better description of your approach than what you did there.
Josh Borg wrote:I didn't understood what creator of topic wants.
benjamin parker wrote:i need help in the recursion part. i am only able to go 1 direction
On top of spaghetti all covered in cheese, there was this tiny ad:
how do I do my own kindlelike thing  without amazon
https://coderanch.com/t/711421/engineering/kindleamazon
