예제 #1
0
        private ListWithDuplicates GetIntersectionsDictFrom(int[] A)
        {
            ListWithDuplicates dict = new ListWithDuplicates();

            for (int i = 0; i < A.Length; i++)
            {
                int          center = i;
                int          radius = A[i];
                Intersection Xn     = new Intersection(center, radius);
                dict.Add(Xn.leftXn, Xn);
            }

            return(dict);
        }
예제 #2
0
        public int solution(int[] A)
        {
            int totalNumIntersections = 0;

            _PreviousXns = new List <Intersection>();

            if (totalNumIntersections > 10000000)
            {
                return(-1);
            }

            //(1) Convert into a Dict of name and Intersections
            _IntersectionsDict = GetIntersectionsDictFrom(A);

            //(2) Sort by LeftXns
            _IntersectionsDict = _IntersectionsDict.SortByKey();

            if (_IntersectionsDict.Count == 0)
            {
                return(0);
            }

            //First (extreme left) value
            int          leftXn       = _IntersectionsDict.ElementAt(0).Key;
            Intersection intersection = _IntersectionsDict.ElementAt(0).Value;

            _PreviousXns.Add(intersection);

            //(2) Now loop left thru right
            for (int i = 1; i < _IntersectionsDict.Count; i++)
            {
                int          nextLeftXn       = _IntersectionsDict.ElementAt(i).Key;
                Intersection nextIntersection = _IntersectionsDict.ElementAt(i).Value;

                //Cases for intersections: Need to check against <wort-case> ALL previous Xns!
                foreach (Intersection prevIntersection in _PreviousXns)
                {
                    string prn0 = "Comparing " + prevIntersection.name + " with " + nextIntersection.name;
                    if (prevIntersection.DoesIntersect(nextIntersection))
                    {
                        prevIntersection.AddIntersectingXn(nextIntersection); //being added to the Xn on the left, as convention
                        totalNumIntersections++;
                    }
                }

                _PreviousXns.Add(nextIntersection);
            }

            //(3) Print Results
            string prn = "";
            List <Intersection> xns = _IntersectionsDict.GetAllValues();

            foreach (Intersection xn in xns)
            {
                prn += xn.name + " Intersects : ";
                foreach (Intersection xn2 in xn._IntersectingXns)
                {
                    prn += xn2.name + " , ";
                }
            }

            return(totalNumIntersections);
        }