Skip to content

Instantly share code, notes, and snippets.

@karmatr0n
Created June 23, 2023 13:44
Show Gist options
  • Save karmatr0n/24ba168891cbec260d92b93d6a31857e to your computer and use it in GitHub Desktop.
Save karmatr0n/24ba168891cbec260d92b93d6a31857e to your computer and use it in GitHub Desktop.
Minimal heaviest subset
def minimal_heaviest_subset(weights)
weights.sort!
half_sum = weights.sum / 2
weights
.each_cons(2)
.filter {|a, b| a+b >= half_sum}
.flatten
.uniq
end
arr = [3,7,5,6,2]
#arr = [5,4,2,5,1,6]
#arr = [3,7,5,6,2,1,8,9,11,12, 4,24,100, 10]
puts minimal_heaviest_subset(arr).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment