[Ruby] Backtracking
\n# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef minimum_incompatibility(nums, k)\n  @sorted_nums, @k = nums.sort.reverse, k\n\n  @nu
```\n# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef minimum_incompatibility(nums, k)\n  @sorted_nums, @k = nums.sort.reverse, k\n\n  @nums_count = nums.count\n  @subset_size = @nums_count/k\n  @subsets = k.times.map{[]}\n  \n  @result = Float::INFINITY\n  \n  return -1 if @sorted_nums.group_by{|num| num}.values.map(&:count).max > @k\n  solver(0)\n  \n  @result\nend\n\ndef solver(i)\n  if i == @nums_count\n    @result = [@result, @k.times.map{|j| @subsets[j].first - @subsets[j].last}.sum].min\n    return true\n  end\n  \n  flag = 0\n  @k.times do |j|\n    if !@subsets[j] || @subsets[j].count < @subset_size && @subsets[j].last != @sorted_nums[i]\n      @subsets[j] << @sorted_nums[i]\n      flag += 1 if solver(i + 1)\n\n      @subsets[j].pop\n    end\n    \n    break if flag >= 2\n  end\n  \n  flag != 0\nend\n```
['Backtracking', 'Ruby']