Gym Sets With Rest
Your workout card lists sets, each tagged with a muscle group: L for legs, C for chest, B for back, and so on. Each time slot holds one set or a rest, and you may do the sets in any order. The coach's rule is that after a set for a group, at least rest other slots (other sets or rests) must pass before that group again. Finish in as few slots as possible.
Your task
Write fewestSlots(sets, rest). sets is a string of uppercase letters, one per set; equal letters are the same muscle group. Between two sets of the same group there must be at least rest other slots. Return the smallest total number of slots, counting rest slots, needed to do every set.
An empty card takes 0 slots. With rest = 0 there is no restriction.
fewestSlots(sets: string, rest: int) → int
Examples
sets = "LLLC" rest = 2
7
One best order is L C _ L _ _ L, where _ is a rest. The three L sets force this length on their own, because the first and last L must be at least 2 × 3 = 6 slots apart, which makes 7 slots in total. C fills a gap but cannot shorten the schedule.
sets = "LLCCAB" rest = 1
6
The order L C L C A B needs no rest, because every L and C is followed by a different group. There are 6 sets, so 6 is the minimum.
Constraints
- 0 ≤ sets.length ≤ 10,000
- sets contains only the letters A to Z
- 0 ≤ rest ≤ 100
Start with brute force
Trying every allowed order slot by slot grows exponentially, so even 30 sets is out of reach. Greedily picking the allowed group with the most sets left is correct, but it builds the whole schedule even though the answer depends only on how many sets the busiest groups have.
Hints
0 of 4 shown- Hint 1
In the first example, ignore C and place only the three L sets. How many slots do they force, whatever else is on the card?
- Hint 2
The group with the most sets builds a frame: with
msets, needsm - 1gaps ofrestslots. Other sets go into the gaps. What if another group has just as many sets? - Hint 3
Each group tied for the most sets adds one slot at the end of the frame, so the frame is
(m - 1) × (rest + 1) + k, wherekis the number of tied groups. When could the answer be larger than the frame? - Hint 4
Count sets per group; let
mbe the largest count andkhow many groups have it. If the other sets overflow the gaps, you never rest and the answer is the number of sets. Returnmax(sets.length, (m - 1) × (rest + 1) + k).
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The group with the most sets is the bottleneck. Laid out as early as the rule allows, its sets form a frame: m - 1 blocks of rest + 1 slots, then the last set. Groups tied for most also need a slot after the last block. Every other set fits into the gaps, because it has fewer than m sets and can be spread across different blocks.
Trace "AAABBBCCD" with rest 1. The counts are A 3, B 3, C 2 and D 1, so m = 3 and k = 2, because A and B tie.
- The frame has
(3 - 1) × (1 + 1) + 2 = 6slots:A B | A B | A B. - C, C and D still need places and the frame is full, so the blocks are widened:
A B C | A B C | A B D. Widening only adds slots between equal groups, so it never breaks the rule. The leftovers are dealt out one group at a time, one set per block, cycling through the blocks, so two sets of one group always land a whole block apart. - No rests are needed, so the answer is the number of sets, 9:
max(9, 6).
There are two cases. If the other sets do not fill the gaps, rests are unavoidable and the frame is the answer. If they overflow the gaps, you never rest and the answer is the number of sets. The max covers both.
function fewestSlots(sets, rest) {
if (sets.length === 0) return 0;
const count = {};
for (const g of sets) count[g] = (count[g] || 0) + 1;
const counts = Object.values(count);
const most = Math.max(...counts);
const tied = counts.filter(c => c === most).length;
// The busiest group(s) build a frame; other sets fill its gaps.
const frame = (most - 1) * (rest + 1) + tied;
return Math.max(sets.length, frame);
}from collections import Counter
def fewest_slots(sets, rest):
if not sets:
return 0
counts = Counter(sets).values()
most = max(counts)
tied = sum(1 for c in counts if c == most)
# The busiest group(s) build a frame; other sets fill its gaps.
frame = (most - 1) * (rest + 1) + tied
return max(len(sets), frame)Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.