Ejemplo n.º 1
0
        public IList<Interval> Insert(IList<Interval> intervals, Interval newInterval)
        {
            var result = new List<Interval>();

            for (int i = 0; i < intervals.Count; i++)
            {
                if (newInterval.start > intervals[i].end)
                {
                    result.Add(intervals[i]);
                }
                else if (newInterval.end < intervals[i].start)
                {
                    result.Add(newInterval);
                    for (int j = i; j < intervals.Count; j++)
                    {
                        result.Add(intervals[j]);
                    }
                    return result;
                }
                else
                {
                    newInterval.start = Math.Min(intervals[i].start, newInterval.start);
                    newInterval.end = Math.Max(intervals[i].end, newInterval.end);
                }
            }

            result.Add(newInterval);
            return result;
        }
Ejemplo n.º 2
0
        public void UnitTest()
        {
            Interval[] interval = new Interval[5];
            for (int index = 1; index < 6; index++)
                interval[index - 1] = new Interval(index, index + 5);
            Console.WriteLine(CanAttendMeetingsByArraySort(interval));

            for (int index = 1; index < 6; index++)
                interval[index - 1] = new Interval(index, 1);
            Console.WriteLine(CanAttendMeetingsByArraySort(interval));
        }
Ejemplo n.º 3
0
        private bool CanAttendMeetingsByBinarySort(Interval[] interval)
        {
            if (interval == null) return true;
            int length = interval.Length;
            if (length == 0) return true;

            BinarySort(interval, 0, length - 1);

            for (int index = 0; index < length - 1; index++)
                if (interval[index].end > interval[index + 1].start)
                    return false;
            return true;
        }
Ejemplo n.º 4
0
        private bool CanAttendMeetingsByArraySort(Interval[] interval)
        {
            if (interval == null) return true;
            int length = interval.Length;
            if (length == 0) return true;

            IComparer revComparer = new IntervalComparer();

            Array.Sort(interval, revComparer);

            for (int index = 0; index < length - 1; index++)
                if (interval[index].end > interval[index + 1].start)
                    return false;
            return true;
        }
Ejemplo n.º 5
0
        private void BinarySort(Interval[] interval, int low, int high)
        {
            if (low > high)
                return;

            int start = low;
            int end = high;
            int key = interval[start].start;
            int temp = 0;

            while (start < end)
            {
                while (start < end && (key.ToString() + interval[end].start.ToString()).CompareTo(interval[end].start.ToString() + key.ToString()) > 0)
                {
                    end--;
                }

                if (start < end)
                {
                    temp = interval[start].start;
                    interval[start].start = interval[end].start;
                    interval[end].start = temp;
                    start++;
                }


                while (start < end && (key.ToString() + interval[start].start.ToString()).CompareTo(interval[end].start.ToString() + key.ToString()) < 0)
                {
                    start++;
                }

                if (start < end)
                {
                    temp = interval[start].start;
                    interval[start].start = interval[end].start;
                    interval[end].start = temp;
                    end--;
                }
            }

            interval[start].start = key;
            BinarySort(interval, low, start - 1);
            BinarySort(interval, start + 1, high);
        }