1555. Chameleon Meetings
A glass terrarium houses chameleons that are red, green or blue; you are given how many of each colour there are as red, green and blue. Whenever two chameleons of different colours bump into each other, both of them instantly change to the third colour (a red and a green that meet become two blues). You may choose which pair meets, in any order and as many times as you like, including not at all.
Return true if some sequence of meetings can leave every chameleon in the terrarium showing one common colour, otherwise false. A terrarium that is already one colour counts as true. The counts are huge, so simulation is out: the answer must be computed in O(1) time and O(1) extra space.
Example 1
- Input:
- red = 2, green = 8, blue = 4
- Output:
- true
- Explanation:
A red and a blue can be paired repeatedly while the two counts stay equal mod 3, ending with everyone green or red, so the answer is true.
Example 2
- Input:
- red = 3, green = 5, blue = 7
- Output:
- false
- Explanation:
The three counts are all different mod 3 and the differences never change mod 3, so no single colour is reachable.
Constraints
0 ≤ red, green, blue ≤ 109
red + green + blue ≥ 1
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(1)
- Space
- O(1)