Jump to content

daemonSiege

Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by daemonSiege

  1. The reason that you are just getting the last number is because of this line... "int score = 0" Because of this, everytime you have a recursive call it is resetting the score to 0. Then the function is just returning 0 + the last number The easiest way to fix this would be to delete that line, change "return score" to "return 0", and change the other return statement to be return a[counter] + Math.min(jumpIt(a, counter + 1), jumpIt(a, counter + 2));
  2. There are 2 ways to accomplish this. The first would be to have a base case and then use deferred operaitons (linear recursion) For example, the base case would return 0 And the recursion would be something like Score + recursive call A better way to solve this would be to create a state variable and add it to the parameters. (tail recursion) For example, your function definition would be something like function(array, current, score) Then the next recursive call would look something like function(array, (current + 1), (score + array[current])) This approach is preferred because i believe java optimizes for tail recursion since only one function call is kept on the stack This is probably pretty confusing if your not familiar with the concept so let me know if u need a better explaination
  3. did u write it in java? i wrote a version of it in scheme just to make sure it worked. but yea go ahead and post it
  4. this is a pretty simple solution to the problem. there are probably more efficient ways to solve it, but my guess is that you just need one that works. ok so you are given an array [a b c d e f g h]. The first thing you must do is make the first element the current value. [ {a} b c d e f g h] Next you will compare b and c. If c < b you will always make c the new current value and add c to the count. [a b {c} d e f g h] If b > c then you will need some more comparisons. you will need to compare b + d and c + e If (c + e) < (b + d) you will make c the new current value and add c to the count Else you will make b the new current value and add b to the count Follow this procedure recursively until the entire list is scanned and return the count. You will also need to add a way to deal with the end of the array since you won't always be able to compare indexes outside the array Hope this helps and let me know if you have any more trouble or if you find a problem with the algorithm
×
×
  • Create New...