There are K light bulbs. All of them are off.
A Flip operation switches the state of a contiguous subset of bulbs. Flip(a,b) means to flip all bulbs x such that a <= x < b. So for example, Flip(4,6) means to flip bulbs 4 and 5, and Flip(3,3) means to flip no bulbs.
Given the value of K and a sequence of N flips, count the number of light bulbs that will be on at the end state. For example,
Input:
K=5000
Flips = [(2000,3000), Flip(1000,3500)]
Output:
1500
Flips = [(2000, 3000), (1000, 3500), (2000,2500), (3000,4500)]
Output : 2500
Expected to solve in O(1) space complexity and O(NlogN) time complexity.
I tried to sort on the list flips but I haven't yet figured out the correct solution.
At the end of the mock , interviewer suggested 1. sorting apporach and 2. using range trees.
Any hints or references to similar problems will be appreciated.