Who can help me figure out what's wrong with this code?


  • 0
    K
        class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            if(prices.size()==1||prices.size()==0)
            return 0;
            // if only one transaction
            int max1=oneMax(prices);
            vector<int> first;
            vector<int> second=prices;
            int temp;
            int max2=0;
            vector<int>::iterator it=second.begin();
            for(int i=0; i<prices.size()-1; i++)
            {
                first.push_back(prices.at(i));
                second.erase(it+i);
                temp=oneMax(first)+oneMax(second);
                if(max2<temp)
                max2=temp;
            }
            return max(max1,max2);
        }
        
        int oneMax(vector<int> &array)
        {
            int current=0;
            int maximum=0;
            for(int i=1; i<array.size(); i++)
            {
                current+=array.at(i)-array.at(i-1);
                if(current<0)
                current=0;
                if(maximum<current)
                maximum=current;
            }
            return maximum;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.