about | help | code help+videos | done | prefs |
Write a method that determines if 'goal' can be met by combining numbers in the 'nums' array using operations identified in the 'ops' string. In each case, you start at zero. You then may (possibly) add or (possibly) subtract numbers in the 'nums' array from your running total until you reach 'goal'. If you are allowed to add, there will be a "+" in the first position of 'ops'. If you are allowed to subtract, there will be a "-" in the second position of 'ops'. If you are not allowed to do an operation there will be a "#" in that operations position. You will always be allowed to do at least one operation. So all the possibilities for 'ops' are "+#", "#-", or "+-". In the first case, you can ONLY add. In the second case you can ONLY subtract. In the last case you can add or subtract. For example, if nums is {4,7,3,1} and ops is "+#" and goal is 10, you would return 'true' because 7+3=10. You would also return 'true' in this case if goal was 1, 3, 4, 5, 7, 8, 10, 11, 12, 14, or 15, but 'false' in all other cases. HINT: You might want to write a helper method to make the recursive call shorter and also (possibly) to include an additional parameter or two if needed. test2021_04_08_HL_backtrackTarget([2, 4, 8, 16, 32, 64, 128, 256], "+#", 256) → true test2021_04_08_HL_backtrackTarget([2, 4, 8, 16, 32, 64, 128, 256], "+#", 510) → true test2021_04_08_HL_backtrackTarget([2, 4, 8, 16, 32, 64, 128, 256], "+#", 512) → false ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 440
Copyright Nick Parlante 2017 - privacy