連續子數組的最大乘積及連續子數組的最大和(Java)


1. 子數組的最大和

輸入一個整形數組,數組里有正數也有負數。數組中連續的一個或多個整數組成一個子數組,每個子數組都有一個和。求所有子數組的和的最大值。例如數組:arr[]={1, 2, 3, -2, 4, -3 } 最大子數組為 {1, 2, 3, -2, 4} 和為8。

解法1(時間復雜度O(N * N)空間復雜度O(1))

求出所有的子數組的和,比較選擇出最大值。利用雙重循環就可以遍歷到所有的子數組。

public static void maxSum1(int arr[]) {
        int max=0,sum;
        for(int i=0;i<arr.length;i++)
        {
            sum=0;
            for(int j=i;j<arr.length;j++)
            {
                //遍歷數組的所有子數組,並將子數組的最大和保存在max中。
                sum+=arr[j];
                max=Math.max(max, sum);
                //max保存最大的子數組的和
            }
        }
        System.out.println(max);
    }

解法2(動態規划時間復雜度O(N)空間復雜度O(1))

遍歷數組,用 sum 保存子數組的和,當 sum<0 時 將 arr[i] 賦值給 sum,用 max 保存最大值。

public static void maxSum(int arr[]){
        int max=0,sum=0;
        for(int i=0;i<arr.length;i++)
        {
            if(sum<=0){
                sum=arr[i];
                //如果 sum<0 重新賦值
            } else {
                sum+=arr[i];
            }
            max=Math.max(sum, max);
            //將最大值保存在max中。
        }
        System.out.println(max);
    }

2. 子數組的最大乘積

輸入一個整形數組,數組里有正數也有負數。數組中連續的一個或多個整數組成一個子數組,每個子數組都有一個和。求所有子數組的和的最大值。例如數組:arr[]={1, 2, 3, -2, 4, 3 } 最大子數組為 {4,3} 積為12。

解法1(時間復雜度O(N * N)空間復雜度 O(1))

跟上面求最大子數組的和類似,利用雙重循環遍歷所有的子數組,求出所有子數組中值最大的。

public static void maxproduct(int arr[]){
        if(arr==null||arr.length==0)
            return;
            //如果數組為 null 或者長度為0直接返回
        int max=0,product=1;
        //max保存子數組的最大乘積,product 用來保存每一個子數組的積
        for(int i=0;i<arr.length;i++) {       
            product=1;
            for(int j=i;j<arr.length;j++){
                product*=arr[j];
                max=Math.max(product, max);
                //max保存最大的子數組乘積
                if(product==0)
                    break;
                //如果當前子數組的乘積為0則以當前數組為頭的后序數組的積全為0不用求。
            }
        }
        System.out.println(max);
    }

解法2(動態規划 時間復雜度O(N )空間復雜度 O(1))

跟上面求最大子數組的和類似,利用雙重循環遍歷所有的子數組,求出所有子數組中值最大的。以arr[i] 結尾的最大值可能由前面的以arr[i-1]結尾的 最大 負值,最大正數,和arr[i] 產生。例如數組:{2,-3,-4}以-4結尾的最大值,就是3*2=-6,與-4 相乘產生的。

public static void maxSubProduct(int arr[]){
        if(arr==null||arr.length==0)
            return ;
        int max=arr[0],min=arr[0],maxend,result=0;
        for(int i=1;i<arr.length;i++) {
            //最大值的來源有三種,如果arr[i]是正數,肯定與前面的最大值相乘得到最大值,
            //如果arr[i]是負數就會與前面的最小值相乘產生最大值。如果前面的為0或者負數,
            //arr[i]本身可能是最大值。
            maxend=Max(max*arr[i],min*arr[i],arr[i]);
            //maxend 保存最大值
            min=Min(max*arr[i], min*arr[i], arr[i]);
            //用於保存最小的負值,為下一個最大值最准備
            max=maxend; 
            result=Math.max(result, max);
        }
        System.out.println(result);
    }

  public static int Max(int a,int b,int c)
    {
    //返回 a b c 中的最大值
        a=Math.max(a, b);
        a=Math.max(a, c);
        return a;
    }
    public static int Min(int a,int b,int c)
    {
    //返回  a b c 中的最小值
        a=Math.min(a, b);
        a=Math.min(a, c);
        return a;
    }https://blog.csdn.net/u013309870/article/details/70144135

 

 

 

轉自:筆試面試算法經典--連續子數組的最大乘積及連續子數組的最大和(Java)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM