首页 > 其他分享 >分治法求最大子数组

分治法求最大子数组

时间:2022-12-05 17:36:26浏览次数:36  
标签:int 分治 mid startIndex low 数组 total 法求

最大子数组(分治法)

解题思路:

将数组分成两份,最大子数组(元素之和最大)只有三种情况:

  • (1)最大子数组在mid左边,即:startIndex和endIndex都在mid左边
  • (2)最大子数组一半在左边,一半在右边,即:startIndex在 mid左边,endIndex在mid右边
  • (3)最大子数组在mid右边,即:startIndex和endIndex都在mid右边

那么比较三种情况下的最大子数组的结果接可以了,最终得出最大子数组
那么继续运用递归,继续给mid左边和右边进行分治求解,将复杂的问题进行简化,以此降低了代码的时间复杂度,提高了程序的运行效率。

代码实现


namespace _分治法求最大子数组

{

    class Program

    {

        struct SubArray

        {

           public  int startIndex;

           public  int endIndex;

           public int total;

        }



        public static void Main(string[] args)

        {









            int[] priceArray = new int[] { 100, 120, 140, 50, 63, 89, 150, 162, 186, 20, 
90, 48, 75, 97, 98 };

            int[] priceFluctuationArray = new int[priceArray.Length - 1]; // 前后相减之后的差组成的数组







            for (int i = 1; i < priceArray.Length; i++)

            {

                priceFluctuationArray[i - 1] = priceArray[i] - priceArray[i - 1];



            }



            SubArray subArray = GetMaxSubArray(0, priceFluctuationArray.Length - 1, 
priceFluctuationArray);



            Console.WriteLine(subArray.startIndex);

            Console.WriteLine(subArray.endIndex+1);







        }

        static SubArray GetMaxSubArray(int low, int high, int[] array) // 这个方法是用来取得array这个数组从low到high之间的最大子数组

        {

            if (low == high)

            {

                SubArray subarray;

                subarray.startIndex= low;

                subarray.endIndex= high;

                subarray.total = array[low];

                return subarray;

            }

            int mid = (low + high) / 2; // 地区间low到mid  ,高区间mid+1 到high



            SubArray  subArray1 =  GetMaxSubArray(low, mid, array);



            SubArray  subArray2 = GetMaxSubArray(mid + 1, high, array);



            // 从low-mid找到最大子数组i-mid

            int total1 = array[mid];

            int startIndex = mid;

            int totalTemp = 0;

            for(int i = mid; i >= low; i--)

            {

                totalTemp+= array[i];

                if (totalTemp > total1)

                {

                    total1 = totalTemp;

                    startIndex = i;

                }

            }

            // 从mid-high找到最大子数组mid+1-j

            int total2;

            total2 = array[mid+ 1];

            int endIndex = mid + 1;

            totalTemp= 0;

            for(int j = mid + 1; j <= high; j++)

            {

                totalTemp+= array[j];

                if(totalTemp> total2)

                {

                    total2 = totalTemp;

                    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;

            }

        }

    }

}

标签:int,分治,mid,startIndex,low,数组,total,法求
From: https://www.cnblogs.com/xiao--liang/p/16952931.html

相关文章

  • 暴力求最大子数组
    最大子数组问题(暴力法)暴力求解:遍历每一个子数组区间,比较大小,并记录最大收益的开始和结束的下标代码实现int[]priceArray=newint[]{100,120,140,50,63,89,15......
  • 数组(2)
    数组作为函数参数往往我们在写代码时,会将数组作为参数传给函数​​实现一个冒泡函数将一个整形数组排序#include<stdio.h>voidbubble_sort(intarr[],intsz){//确定冒......
  • JS获取数组中元素的最大值
    方法1:Math.max.apply()Math.max()方法默认接收多个参数并返回最大值,而apply()方法接收一个数组,将数组中的每一项作为参数传给调用函数,搭配使用可以得到最大值。const......
  • 数组循环的时候判断对应的订单id是否一致,一致的话重新赋值
    $param_data['num']=1000;$list=(new\app\common\model\Order())->get_user_list($param_data);$list=$list->toArray();$list_data=$list['data'];$not_read_msg......
  • 使用list和数组保存数据的差别
    在上位机开发曲线供能时遇到一个疑惑的问题,但又感觉这个问题太基础,想求证一下。需求:一共有1000个模拟量数据,每个数据记录600个点作为一组数据曲线,那么这1000个模拟量需要......
  • JAVASCRIPT数组小结
    ​数组是值的有序集合。每个值叫做一个元素,而每个元素在数组中有一个位置,以数字表示,称为索引。JavaScript数组是无类型的,数组元素可以是任意类型,并......
  • js多个(N)个数组的的元素组合排序算法,多维数组的排列组合或多个数组之间的排列组合
    现在有一批手机,其中颜色有['白色','黑色','金色','粉红色'];内存大小有['16G','32G','64G','128G'],版本有['移动','联通','电信'],要求写一个算法,实现[['白色','16G','移动'......
  • 数组排序,自己内部会调整,数组也是引用类型
    Java的基本数据类型有8种,分别是:byte(位)、short(短整数)、int(整数)、long(长整数)、float(单精度)、double(双精度)、char(字符)和boolean(布尔值)。数组是引用类型 int[] arr2 = {......
  • React中的函数组件详解
    转载来自(47条消息)React中的函数组件详解_『荼』的博客-CSDN博客_react函数组件1.创建方式//写法一constHello=(props)=>{return<div>{props......
  • 力扣 leetcode 209. 长度最小的子数组
    问题描述给定一个含有n个正整数的数组和一个正整数target。找出该数组中满足其和≥target的长度最小的连续子数组[numsl,numsl+1,...,numsr-1,numsr],并返......