Cover tables with 3-color ribbons
viaLeetCode
Problem You have an unlimited row of tables and ribbons of 3 colours with counts a, b, c. Each covered table uses one ribbon, and no two adjacent tables may use the same colour. Tables are covered left to right consecutively. Return the maximum number of tables that can be covered.
Input / Output
- Input: three non-negative integers a, b, c.
- Output: maximum number of consecutively covered tables.
Constraints
- Counts up to 10^9 — the answer must be O(1)/greedy, not simulation for large inputs.
Example
- a=5, b=1, c=1 → 5 (e.g. A B A C A; the two singles separate the dominant colour: 2*(b+c)+1).
- a=2, b=2, c=2 → 6 (all ribbons usable).
Expected approach
- Let mx = max(a,b,c) and rest = a+b+c-mx. If mx > rest+1 the dominant colour saturates: answer = 2*rest + 1. Otherwise every ribbon can be placed: answer = a+b+c. Equivalent greedy: repeatedly pick the most frequent colour different from the previous one (max-heap) — useful to explain correctness.
asked …