MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / fill

Function fill

partition_to_k_equal_sum_subsets_698/solution.go:37–70  ·  view source on GitHub ↗
(subsets []int, idx int, nums []int, target int)

Source from the content-addressed store, hash-verified

35}
36
37func 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.

Callers 1

canPartitionKSubsetsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected