Distributing numbers into the minimal number of categories based on their distance

Viewed 22

You have a collection of some numbers between 1 to 100. There can be multiple instances of the same number.

The task is to distribute these numbers to as few letters as possible, but ensuring that numbers that are 6 away from each other are associated with different letters.

Here are a few examples.

Case 1, an easy case.

Numbers: 1, 10, 20, 30, 40, 45, 46

Can be distributed to letters A, B and C. This is because most of the numbers are far apart and can be put on letter A but 45 and 46 clash with each other and with 40.

A:1 , 10 ,20, 30, 40

B: 45

C: 46

Case 2. More complicated

Numbers: 1,5,7,10,13,15,20,21,27,27,27,27

Letters: A:1, 7,13,20,27

B: 5, 15,21,27

C: 10,27

D: 27

I have been trying a few methods in Python, but I am not quite sure how to do this. I have tried organizing 'clashes' into networks and analyzing these number lines using sliding windows.

I think there must be an easier way to do this and was wondering if anyone had any ideas. They would be much appreciated.

0 Answers
Related