C#学习笔记——分治算法

分治法的设计思想是: 将一个难以直接解决的大问题,分割成一些规模较小的相同问题,以便各个击破,分而治之。

问题:求数列的最大字段和

股票问题

日期1234567891011121314151617
价格10055110851051028663811019410610179949097
变化-4555-2520-3-16-231820-712-5-2215-47

从0索引开始 数组中最小的值 到 数组中最大的值 之和的一个数组
//例如 55 – 135 这段数组就是最大子数组

 //价格数组
 static int[] priceArray = { 100, 55, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97, 80, 120, 135, 90 };
 static int[] priceFluctuationArray = new int[priceArray.Length - 1];    //变化数组	

暴力求解方式

        int total = priceFluctuationArray[0];   //默认数组第一个元素是最大子数组
        int startIndex = 0;
        int endIndex = 0;
        for (int i = 0; i < priceFluctuationArray.Length; i++)
        {
            for (int j = i; j < priceFluctuationArray.Length; j++)
            {
                int tempSum = 0;
                for (int k = i; k < j + 1; k++)
                {
                    tempSum += priceFluctuationArray[k];
                }

                if (tempSum > total)
                {
                    total = tempSum;
                    startIndex = i;
                    endIndex = j;
                }
            }
        }

        Debug.Log(string.Format("买入日期:{0} 卖出日期:{1}", startIndex, (endIndex + 1)));

分治法求解

    private SubArray FenZi(int low, int high, int[] array)
    {
        for (int i = 1; i < priceArray.Length; i++)
        {
            //变化价格 == 价格数组第i个 - 价格数据第i - 1个;
            priceFluctuationArray[i - 1] = priceArray[i] - priceArray[i - 1];
        }

        if (low == high)
        {
            SubArray subArray;
            subArray.startIndex = low;
            subArray.endIndex = high;
            subArray.total = array[low];
            return subArray;
        }

        int mid = (low + high) / 2;
        SubArray subArray1 = FenZi(low, mid, array);
        SubArray subArray2 = FenZi(mid + 1, high, array);

        int total1 = array[mid];
        int startIndex = mid;
        int temptotal = 0;
        for (int i = mid; i >= low; i--)
        {
            temptotal += array[i];
            if (temptotal > total1)
            {
                total1 = temptotal;
                startIndex = i;
            }
        }

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

        SubArray subArray3;
        subArray3.startIndex = startIndex;
        subArray3.endIndex = endIndex;
        subArray3.total = total1 + 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;
        }
      }
      
    struct SubArray
    {
        public int startIndex;
        public int endIndex;
        public int total;
    }

调用


    void Start()
    {
       for (int i = 1; i < priceArray.Length; i++)
        {
             //变化价格 == 价格数组第i个 - 价格数据第i - 1个;
            //-45 55 -25 20  -3  -16 -23 18  20  -7  12  -5  -22 15  -4  7   -17 40  15 -45
            priceFluctuationArray[i-1] = priceArray[i] - priceArray[i - 1];
        }
        SubArray subArray = FenZi(0, priceFluctuationArray.Length - 1, priceFluctuationArray);
        Debug.Log(string.Format("买入日期:{0} 卖出日期:{1}", subArray.startIndex, (subArray.endIndex + 1)));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值