recursion - Need help to spot flaws in my java code -


I am working on the work of a school and my next question is:

Given (with different or equal length) sticks, and the length of the given length is done to investigate the minimum number required to make a long stick.

For example, to make a long stick of length 5 from length 1, 1, 1, 3, the output will be 3. Note: Sticks can not be reused.

However, if given length, it is impossible to make Output-1 For example, to make a long stick of length 5, 3 sticks of length 1,2,6 have been given, output - 1

I have the following code, which has passed all public trial cases. However, I failed in the matters of personal examination, which I could not understand my mistake.

This is my code:

  import java.util. *; Class results {static int min = 100000; Public static integer solution (int piece, int curous, int lane, int newstick) {// For implementation (int i = 1; i  = Stick.data.length) {break; } And {Hull (pos + i, curr + stick data. [Pin + i], lane, numstk + 1); }} And if (currSum == lane) {if (numstk & lt; min) {min = numStk; }}} Return minutes; }} Class stick {static int [] data; Public static zero main (string [] args) {scanner sc = new scanner (System.in); Int sticks = sc.nextInt (); Data = new int [rod]; Int len ​​= sc.nextInt (); For (int i = 0; i  

I notice about your code:

  1. Bug: In the main,

    Result.solve (i, 0, len, 1);

    Believes I took the stick (hence the numsticks = 1 in the logic list), but currSum is given as 0. It should not be data [i]

  2. Better code quality: currSum>


Comments