【leetcode】 Unique Binary Search Trees (middle)☆

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

找数字连续最大乘积子序列。

思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设为初始值。

对于两个0之间的数若有奇数个负数,那则有两种情况,第一种是不要第一个负数和之前的值,第二种是不要最后一个负数和之后的值,用negtiveFront和negtiveBack表示。没有负数就是不要第一个负数和之前的值的情况。

int maxProduct(int A[], int n) {
        if(n == 0)
            return 0;

        int MaxAns = A[0];
        int negtiveFront = (A[0] == 0) ? 1 : A[0];
        int negtiveBack = (A[0] < 0) ? 1 : 0;

        for(int i = 1; i < n; i++)
        {
            if(A[i] == 0)
            {
                MaxAns = (MaxAns > 0) ? MaxAns : 0;
                negtiveFront = 1;
                negtiveBack = 0;
            }
            else if(A[i] < 0)
            {
                negtiveFront *= A[i];
                MaxAns = max(negtiveFront, MaxAns);
                if(negtiveBack == 0)
                {
                    negtiveBack = 1;
                }
                else
                {
                    negtiveBack *= A[i];
                    MaxAns = max(negtiveBack, MaxAns);
                }
            }
            else
            {
                negtiveFront *= A[i];
                negtiveBack *= A[i];
                MaxAns = max(negtiveFront, MaxAns);
                if(negtiveBack > 0)
                {
                    MaxAns = max(negtiveBack, MaxAns);
                }

            }
        }

        return MaxAns;
    }

答案的思路:同时维护包括当前数字A[k]的最大值f(k)和最小值g(k)

f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )

再用一个变量Ans存储所有f(k)中最大的数字就可以了

int maxProduct2(int A[], int n) {
        if(n == 0)
            return 0;

        int MaxAns = A[0]; //包括当前A【i】的连续最大乘积
        int MinAns = A[0]; //包括当前A【i】的连续最小乘积
        int MaxSoFar = A[0]; //整个数组的最大乘积

        for(int i = 1; i < n; i++)
        {
            int MaxAnsTmp = MaxAns;
            int MinAnsTmp = MinAns;
            MaxAns = max(MaxAnsTmp * A[i], max(MinAnsTmp * A[i], A[i]));
            MinAns = min(MinAnsTmp * A[i], min(MaxAnsTmp * A[i], A[i]));
            MaxSoFar = max(MaxSoFar, MaxAns);

        }

        return MaxSoFar;
    }
时间: 2024-10-26 14:13:50

【leetcode】 Unique Binary Search Trees (middle)☆的相关文章

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

【LeetCode】Unique Binary Search Trees (2 solutions)

Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Base case: n==0, n==1时,f

【leetcode】 Unique Binary Search Trees II (middle)☆

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 这次的题目要求是得到所有的树. 我的思路: 用f[

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/ 题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below.

【leetcode】Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root! 不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者

【leetcode】Unique Binary Search Trees (#96)

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, given n = 3, there are a total of 5 unique BST's. 1         3     3      2      1     \       /     /      / \      \      3     2     1      1  

【树】Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: 找到一个数作为根结点,剩余的数分别

【树】Unique Binary Search Trees

题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: 依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子

【leetcode刷题笔记】Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:递归的枚举1~n的每个节点为根节点,然后递归