[LeetCode] Traffic Signal Color

3894. Traffic Signal Color

You are given an integer timer representing the remaining time (in seconds) on a traffic signal.

The signal follows these rules:

  • If timer == 0, the signal is "Green"
  • If timer == 30, the signal is "Orange"
  • If 30 < timer <= 90, the signal is "Red"

Return the current state of the signal. If none of the above conditions are met, return "Invalid".

1
2
3
4
5
6
class Solution {
public:
string trafficSignal(int timer) {
return !timer ? "Green" : timer == 30 ? "Orange" : 30 < timer and timer <= 90 ? "Red" : "Invalid";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/traffic-signal-color/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.