Вот моя попытка найти решение:
class ArrayPartition
def self.partition_lengths(length, minimum, maximum)
if length <= maximum
return [length]
end
group_size = maximum
groups = []
while groups.empty? || groups.last < minimum
groups = []
remaining = length
while remaining > group_size
groups << group_size
remaining -= group_size
end
groups << remaining
group_size -= 1
end
# Redistribute evenly
avg_group_size = (length / groups.size.to_f).round
groups = []
remaining = length
while remaining > maximum
groups << avg_group_size
remaining -= avg_group_size
end
groups << remaining
groups.sort
end
end
Тест RSpec:
RSpec.describe ArrayPartition do
it 'partitions an array into optimal groups with min and max elements' do
expect(ArrayPartition.partition_lengths(5, 5, 10)).to eq [5]
expect(ArrayPartition.partition_lengths(6, 5, 10)).to eq [6]
expect(ArrayPartition.partition_lengths(7, 5, 10)).to eq [7]
expect(ArrayPartition.partition_lengths(10, 5, 10)).to eq [10]
expect(ArrayPartition.partition_lengths(11, 5, 10)).to eq [5, 6]
expect(ArrayPartition.partition_lengths(12, 5, 10)).to eq [6, 6]
expect(ArrayPartition.partition_lengths(13, 5, 10)).to eq [6, 7]
expect(ArrayPartition.partition_lengths(16, 5, 10)).to eq [8, 8]
expect(ArrayPartition.partition_lengths(20, 5, 10)).to eq [10, 10]
expect(ArrayPartition.partition_lengths(21, 5, 10)).to eq [7, 7, 7]
expect(ArrayPartition.partition_lengths(22, 5, 10)).to eq [7, 7, 8]
expect(ArrayPartition.partition_lengths(5, 10, 20)).to eq [5]
expect(ArrayPartition.partition_lengths(10, 10, 20)).to eq [10]
expect(ArrayPartition.partition_lengths(15, 10, 20)).to eq [15]
expect(ArrayPartition.partition_lengths(20, 10, 20)).to eq [20]
expect(ArrayPartition.partition_lengths(21, 10, 20)).to eq [10, 11]
expect(ArrayPartition.partition_lengths(30, 10, 20)).to eq [15, 15]
expect(ArrayPartition.partition_lengths(40, 10, 20)).to eq [20, 20]
expect(ArrayPartition.partition_lengths(41, 10, 20)).to eq [13, 14, 14]
expect(ArrayPartition.partition_lengths(42, 10, 20)).to eq [14, 14, 14]
expect(ArrayPartition.partition_lengths(43, 10, 20)).to eq [14, 14, 15]
expect(ArrayPartition.partition_lengths(45, 10, 20)).to eq [15, 15, 15]
expect(ArrayPartition.partition_lengths(50, 10, 20)).to eq [16, 17, 17]
expect(ArrayPartition.partition_lengths(55, 10, 20)).to eq [18, 18, 19]
expect(ArrayPartition.partition_lengths(60, 10, 20)).to eq [20, 20, 20]
expect(ArrayPartition.partition_lengths(61, 10, 20)).to eq [15, 15, 15, 16]
end
end