(subsets []int, idx int, nums []int, target int)
| 35 | } |
| 36 | |
| 37 | func fill(subsets []int, idx int, nums []int, target int) bool { |
| 38 | if idx < 0 { |
| 39 | return true |
| 40 | } |
| 41 | |
| 42 | // choose a value from nums to try to slot into each subset |
| 43 | pick := nums[idx] |
| 44 | idx-- |
| 45 | |
| 46 | // try to fill each subset |
| 47 | for i := 0; i < len(subsets); i++ { |
| 48 | if subsets[i]+pick <= target { |
| 49 | subsets[i] += pick |
| 50 | |
| 51 | // explore if the subsets current value + chosen value is <= target |
| 52 | if fill(subsets, idx, nums, target) { |
| 53 | // if call to fill is true, we were able to partition successfully |
| 54 | // if not, continue on the search |
| 55 | return true |
| 56 | } |
| 57 | |
| 58 | // un-choose the value for the current subset |
| 59 | subsets[i] -= pick |
| 60 | } |
| 61 | |
| 62 | // if current subset became 0 from the un-choose, then break |
| 63 | // this keeps all unfilled subsets on the end and reduces work |
| 64 | if subsets[i] == 0 { |
| 65 | break |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | // First take on the problem without peeking solution. |
no outgoing calls
no test coverage detected