/*
         *  The array of tasks is given.
         *  Find the number of tasks that are performed simultaneusly.
         *
         *  Complexety: O(n*log(n))
         */
        public static List <Greedy_Task> FindOverlapNumber(Greedy_Task[] tasks)
        {
            List <Greedy_Task> overlapped = new List <Greedy_Task>();

            if (tasks.Length <= 0)
            {
                return(overlapped);
            }

            if (tasks.Length == 1)
            {
                overlapped.Add(tasks[0]);
                return(overlapped);
            }

            Greedy_Task[] starts = new Greedy_Task[tasks.Length];
            for (int i = 0; i < tasks.Length; i++)
            {
                starts[i] = tasks[i];
            }

            Greedy_Task[] ends = new Greedy_Task[tasks.Length];
            for (int i = 0; i < tasks.Length; i++)
            {
                ends[i] = tasks[i];
            }

            Array.Sort(starts, new Number_ComparerStarttime());
            Array.Sort(ends, new Number_ComparerEndtime());

            int startCoursor  = 1;
            int endCoursor    = 0;
            int currentNumber = 1;
            int maxNumber     = 1;

            HashSet <Greedy_Task> tmpSet = new HashSet <Greedy_Task>();

            tmpSet.Add(starts[0]);

            while (startCoursor < tasks.Length && endCoursor < tasks.Length)
            {
                if (starts[startCoursor].Starttime < ends[endCoursor].Endtime)
                {
                    currentNumber++;
                    tmpSet.Add(starts[startCoursor]);

                    if (currentNumber > maxNumber)
                    {
                        maxNumber = currentNumber;

                        overlapped.Clear();
                        foreach (Greedy_Task task in tmpSet)
                        {
                            overlapped.Add(task);
                        }
                    }

                    startCoursor++;
                }
                else
                {
                    currentNumber--;
                    tmpSet.Remove(ends[endCoursor]);

                    endCoursor++;
                }
            }

            return(overlapped);
        }
        public int CompareTo(object taskToCompare)
        {
            Greedy_Task task = taskToCompare as Greedy_Task;

            return(this.endtime.CompareTo(task.Endtime));
        }