Showing Wrong Answer even though other compiler is showing expected output.


  • 0
    M

    For the test case [2,1], the OJ is showing my answer to be coming out as 15, while the expected output is 0. On other compiler my output is coming 0.

    class Solution {
    public:
    int maxProfit(vector<int> &prices) {
        int i=0;
        int s=0;
        int b=0;
        // int flag=0;
        int profit=0;
        int flag2=0;
        while(i<=int(prices.size())-2)
        {
            while(prices[i]>=prices[i+1])
            {
                i++;
                // flag=1;
                if(i>=int(prices.size()))
                {
                    flag2=1;
                    break;
                }
            }
            if(flag2==1)
            {
                break;
            }
            b=i;
            while(prices[i]<prices[i+1])
            {
                i++;
                // flag=2;
                if(i>=int(prices.size())-1)
                {
                    break;
                }
            }
            s=i;
            profit = profit+(int(prices[s])-int(prices[b]));
        }
        return profit;
    }};

Log in to reply
 

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