Beispiel #1
0
        static void Main(string[] args)
        {
            int[] arrary = new int[] { 100, 113, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97 };

            int[] minRes = new int[arrary.Length - 1];

            for (int i = 0; i < arrary.Length - 1; i++)
            {
                minRes[i] = arrary[i + 1] - arrary[i];
            }

            MaxSubArray subArray = GetMaxSubArray(0, minRes.Length - 1, minRes);

            Console.WriteLine(subArray.startIndex);
            Console.WriteLine(subArray.endIndex);
            Console.WriteLine(subArray.startIndex + "天买入,在第" + subArray.endIndex + 1 + "卖出");
        }
Beispiel #2
0
        /// <summary>
        /// 递归 分治
        /// </summary>
        /// <param name="low"></param>
        /// <param name="high"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public MaxSubArray MaxSubArray2(int low, int high, int[] array)
        {
            //结束 条件
            if (low == high)
            {
                MaxSubArray maxSubArray;
                maxSubArray.startIndex = low;
                maxSubArray.endIndex   = high;
                maxSubArray.total      = array[low];
                return(maxSubArray);
            }

            int mid = (low + high) / 2;                                 //分区

            MaxSubArray subArray1 = MaxSubArray2(low, mid, array);      //左边的     情况1
            MaxSubArray subArray2 = MaxSubArray2(mid + 1, high, array); //右边的   情况2


            //   情况3
            // 左边 寻找
            int total      = array[low];
            int startIndex = low;
            int temp       = 0;

            for (int i = low; i <= mid; i++)
            {
                temp += array[i];
                if (temp > total)
                {
                    total      = temp;
                    startIndex = i;
                }
            }

            //右边 寻找
            int total2   = array[mid + 1];
            int endIndex = mid + 1;

            temp = 0;
            for (int i = mid + 1; i < high; i++)
            {
                total2 += array[i];
                if (temp > total2)
                {
                    total2   = temp;
                    endIndex = i;
                }
            }

            //   情况3
            MaxSubArray SubArray3;

            SubArray3.startIndex = startIndex;
            SubArray3.endIndex   = endIndex;
            SubArray3.total      = total + total2;

            if (subArray1.total > subArray2.total && subArray1.total > SubArray3.total)
            {
                return(subArray1);
            }
            else if (subArray2.total > subArray1.total && subArray2.total > SubArray3.total)
            {
                return(subArray2);
            }
            else
            {
                return(SubArray3);
            }
        }
Beispiel #3
0
        public static MaxSubArray GetMaxSubArray(int low, int high, int[] arrary)
        {
            if (low == high)
            {
                MaxSubArray maxSubArray;
                maxSubArray.startIndex = low;
                maxSubArray.endIndex   = high;
                maxSubArray.total      = arrary[low];
                return(maxSubArray);
            }

            int mid = (low + high) / 2;//低 区间[low,mid]  高区间 [mid+1,high]

            MaxSubArray maxSubArray1 = GetMaxSubArray(low, mid, arrary);

            MaxSubArray maxSubArray2 = GetMaxSubArray(mid + 1, high, arrary);

            //从[low,mid] 找到最大子数组[i,mid]  从mid 到0(low)遍历
            int total      = arrary[mid];
            int tempTot    = 0;
            int StartIndex = mid;

            for (int i = mid; i >= low; i--)
            {
                tempTot += arrary[i];
                if (tempTot > total)
                {
                    total      = tempTot;
                    StartIndex = i;
                }
            }

            //从[mid+1,hight] 找到最大子数组[mid+1,j];
            int total2   = arrary[mid + 1];
            int endIndex = mid + 1;

            tempTot = 0;
            for (int i = mid + 1; i <= high; i++)
            {
                tempTot += arrary[i];
                if (tempTot > total2)
                {
                    total2   = tempTot;
                    endIndex = i;
                }
            }

            MaxSubArray subArray3;

            subArray3.startIndex = StartIndex;
            subArray3.endIndex   = endIndex;
            subArray3.total      = total + total2;

            if (maxSubArray1.total >= maxSubArray2.total && maxSubArray1.total >= subArray3.total)
            {
                return(maxSubArray1);
            }
            else if (maxSubArray2.total > maxSubArray1.total && maxSubArray2.total > subArray3.total)
            {
                return(maxSubArray2);
            }
            else
            {
                return(subArray3);
            }
        }