[LeetCode] Minimize the Maximum Waiting Time at Synchronized Traffic Lights

4025. Minimize the Maximum Waiting Time at Synchronized Traffic Lights

You are given an integer period and an integer array lights, where lights[i] is the duration, in seconds, of the green phase of the i^th traffic light.

At time 0, every traffic light starts at the beginning of its green phase. Their cycles are synchronized: every traffic light starts a new cycle at the same time, and every cycle lasts exactly period seconds. Therefore, the red phase of the i_th traffic light lasts for period - lights[i] seconds.

You are also given an integer array arrivalTime, where arrivalTime[j] is the arrival time, in seconds, of the j^th car.

Each car must be assigned to exactly one traffic light. Multiple cars may be assigned to the same traffic light. Any number of cars may cross the same traffic light simultaneously while it is green. Cars do not block or delay one another.

For a car j assigned to the i^th traffic light, let r = arrivalTime[j] % period. If r < lights[i], its waiting time is 0. Otherwise, its waiting time is period - r.

The penalty of an assignment is the maximum waiting time among all cars.

Return an integer denoting the minimum possible penalty.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int minPenalty(int period, vector<int>& lights, vector<int>& arrivalTime) {
int ma = *max_element(begin(lights), end(lights));
int res = 0;
for(auto& t : arrivalTime) {
t %= period;
if(t < ma) continue;
res = max(res, period - t);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/minimize-the-maximum-waiting-time-at-synchronized-traffic-lights/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.