about | help | code help+videos | done | prefs |
Write a method that returns 'true' if it is possible to reach the 'target' number of gallons by filling some or all of the buckets in the 'sizes' array. Return 'false' otherwise. For example, if the sizes array is {5,7,12,3} that represents having buckets of 5 gallons, 7 gallons, 12 gallons, and 3 gallons. Note that there are many possible targets that can be reached with these buckets such as: 5, 7, 12, 3, but also 8 (5+3), 10 (7+3), 15 (12+3), 27 (5+7+12+3), and other targets. But notice that it is not possible to have 11 gallons from these buckets (for example). It is also not possible to have any amount greater than 27 gallons since it takes all the buckets to get to 27 gallons. Your solution may use recursion, but it is NOT required. However, if you do decide to write a recursive solution, a helper method could be VERY useful! In fact, this is nicely solved using a single helper method with backtrack recursion. finalExam2014Buckets([1, 10, 100, 1000], 1111) → true finalExam2014Buckets([1, 10, 100, 1000], 123) → false finalExam2014Buckets([5, 7, 20, 50], 32) → true ...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: 420
Copyright Nick Parlante 2017 - privacy