Skip to content
bradendubois edited this page Oct 7, 2021 · 7 revisions

Coloring Socks

ID: color

Difficulty: 2.2

CPU Time: 1 second

Memory: 1024 MB

Solution

A greedy solution works for this problem: read in all the socks, and sort them. Then, take a machine and, while it is either not full or the absolute value between the current sock and the first sock put into the machine is <= K, increment the number of socks in the machine. If the machine is full or the difference is too large, increment the number of machines used by 1.

Clone this wiki locally