最大连续乘积子数组

一、题目
//给定浮点数数组,任意取出数组中的若干个连续的数相乘,请找出最大的子数组。
//比如:{-2.5,4,0,3,0.5,8,-1},则取出的最大乘积子数组为{3,0.5,8},也就是3,0.5,8这三个数是连续的且乘积是最大的。

二、方法一:暴力求解(O(n^2))

static double a[] = new double[] { -2.5, 4, 0, 3, 0.5, 8, -1 };
static double maxResult;
//暴力求解
public static void getResult1() {
        for (int i = 0; i < a.length; i++) {
            double x = 1;
            for (int j = i; j < a.length; j++) {
                x *= a[j];
                if (x > maxResult) {
                    maxResult = x;
                }
            }
        }
        System.out.println(maxResult);
}

三、方法二:动态规划(O(n))

因为数组中包括负数
设maxEnd表示以a[i]结尾的最大连续子数组的乘积值。
设minEnd表示以a[i]结尾的最小连续子数组的乘积值。
可得到状态转移方程:
1、maxEnd=max{ max{maxEnd*a[i],minEnd*a[i]},a[i]);
(
意思是:以a[i]结尾的最大(小)连续子数组的乘积值是:A,B,C中的最大值。
A:以a[i-1]结尾的最大连续子数组的乘积值
B:以a[i-1]结尾的最小连续子数组的乘积值
C:a[i]自身的值
)
2、minEnd=min{ min{minEnd*a[i],minEnd*a[i]},a[i]};
由状态转移方程可以写出代码:

    static double a[] = new double[] { -2.5, 4, 0, 3, 0.5, 8, -1 };
    static double maxResult;
    static double maxEnd;
    static double minEnd;

    public static void main(String[] args) {
        getResult2();
    }
    /**
     * 动态规划法
     */
    public static void getResult2() {
        for (int i = 1; i < a.length; i++) {
            double end1 = maxEnd * a[i];
            double end2 = minEnd * a[i];
            maxEnd = Math.max(Math.max(end1, end2), a[i]);
            minEnd = Math.min(Math.min(end1, end2), a[i]);
            maxResult = Math.max(maxResult, maxEnd);
        }
        System.out.println(maxResult);
    }

We often hear of people breaking down from overwork,but in nine cases out of ten they are really suffering from worry or anxiety.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值