Пример #1
0
        /// <summary>
        /// This method assumes there is at least one positive number in the array.
        /// Otherwise it will break </summary>
        /// <param name="arr">
        /// @return </param>
        public virtual Triplet kadane(int[] arr)
        {
            int sum    = 0;
            int cStart = 0;
            int mStart = 0;
            int end    = 0;
            int maxSum = int.MinValue;

            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
                if (sum <= 0)
                {
                    sum    = 0;
                    cStart = i + 1;
                }
                else
                {
                    if (sum > maxSum)
                    {
                        maxSum = sum;
                        mStart = cStart;
                        end    = i;
                    }
                }
            }
            Triplet p = new Triplet();

            p.sum   = maxSum;
            p.start = mStart;
            p.end   = end;
            return(p);
        }
Пример #2
0
 public virtual Triplet findTriplet(int[] input, int sum)
 {
     Array.Sort(input);
     for (int i = 0; i < input.Length - 2; i++)
     {
         int start   = i + 1;
         int end     = input.Length - 1;
         int new_sum = sum - input[i];
         while (start < end)
         {
             if (new_sum == input[start] + input[end])
             {
                 Triplet t = new Triplet(this);
                 t.a = input[i];
                 t.b = input[start];
                 t.c = input[end];
                 return(t);
             }
             if (new_sum > input[start] + input[end])
             {
                 start++;
             }
             else
             {
                 end--;
             }
         }
     }
     return(null);
 }
Пример #3
0
        public virtual IList <int?> mergeUsingHeap(IList <IList <int?> > chunks)
        {
            IList <int?>            result = new List <int?>();
            PriorityQueue <Triplet> queue  = new PriorityQueue <Triplet>();

            //add first element of every chunk into queue
            for (int i = 0; i < chunks.Count; i++)
            {
                Triplet p = new Triplet(this);
                p.pos   = i;
                p.val   = chunks[i][0];
                p.index = 1;
                queue.add(p);
            }

            while (!queue.Empty)
            {
                Triplet p = queue.poll();
                result.Add(p.val);
                if (p.index < chunks[p.pos].Count)
                {
                    p.val    = chunks[p.pos][p.index];
                    p.index += 1;
                    queue.add(p);
                }
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// If it is not guaranteed that tour exists then once you get
        /// result of kadanewrap make an actual trip to see if value is positive </summary>
        /// <returns> -1 if no solution exists otherwise returns gas station at which to start. </returns>
        public virtual int startTour1(int[] gasAvailable, int[] gasRequired)
        {
            int[] diff = new int[gasAvailable.Length];
            for (int i = 0; i < diff.Length; i++)
            {
                diff[i] = gasAvailable[i] - gasRequired[i];
            }

            bool allNegative = true;

            for (int i = 0; i < diff.Length; i++)
            {
                if (diff[i] >= 0)
                {
                    allNegative = false;
                    break;
                }
            }

            if (allNegative)
            {
                return(-1);
            }

            KadaneWrapArray kwa = new KadaneWrapArray();
            Triplet         t   = kwa.kadaneWrap(diff);
            //make sure this solution leads to answer
            int i1     = t.start;
            int netGas = 0;

            do
            {
                netGas += diff[i1];
                i1      = (i1 + 1) % diff.Length;
                if (netGas < 0)
                {
                    return(-1);
                }
            } while (i1 != t.start);

            return(t.start);
        }
Пример #5
0
        public virtual Triplet kadaneWrap(int[] arr)
        {
            Triplet straightKadane = kadane(arr);
            int     sum            = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                sum   += arr[i];
                arr[i] = -arr[i];
            }
            Triplet wrappedNegKadane = kadane(arr);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = -arr[i];
            }
            if (straightKadane.sum < sum + wrappedNegKadane.sum)
            {
                straightKadane.sum   = wrappedNegKadane.sum + sum;
                straightKadane.start = wrappedNegKadane.end + 1;
                straightKadane.end   = wrappedNegKadane.start - 1;
            }
            return(straightKadane);
        }