How to get overlap range of two range

后端 未结 4 1553
眼角桃花
眼角桃花 2020-12-21 09:25

I have the following ranges in interval [1-15]

I want to find the overlap ranges between people 1 & 2.

Person1 [1, 3] [5, 10] Person2 [2, 4] [8, 15]

4条回答
  •  醉酒成梦
    2020-12-21 10:03

    I don't understand how you're looping by 'person1's range, then by person2's ranges' - I'm not sure what that means without seeing the code.

    I can't see how you would get better than O(n), but you can iterate through the range only once. A better data structure might be a bool[] or BitArray.

    var person1 = new bool[15] { /* set values */ };
    var person2 = new bool[15] { /* set values */ };
    
    var overlap = new bool[15];
    
    for (int i = 0; i < 15; i++)
    {
        overlap[i] = person1[i] && person2[i];
    }
    

提交回复
热议问题