示例#1
0
        public static void Main(string[] args)
        {
            KadaneWrapArray kwa = new KadaneWrapArray();

            int[] input  = new int[] { 12, -2, -6, 5, 9, -7, 3 };
            int[] input1 = new int[] { 8, -8, 9, -9, 10, -11, 12 };
            Console.WriteLine(kwa.kadaneWrap(input));
            Console.WriteLine(kwa.kadaneWrap(input1));
        }
示例#2
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);
        }