solution2
I am not able to understand how second solution has O(n) time complexity,
Is it a typo?
for (int i = light_num - 1; i > 0; i--) {
street_pos.erase(lights[i]);
auto high_it = street_pos.upper_bound(lights[i]);
int high = *high_it;
int low = *(--high_it);
max_gap = std::max(max_gap, high - low);
gaps[i - 1] = max_gap;
}
In the given code we can see that upper_bound operation on set is O(log n) and this operation is done light_num
of times, which is equivalent to O(nlog n).