HELP!!! Whether the sampl test is wrong !!!


  • 0
    Y

    this is my code ,but i have pass the 170’s code ,but why that is error

    test no.170
    Input: [3,3,5,0,0,3,1,4]
    Output: 8
    Expected: 6
    my idea is res =5-3 + 3-0 +4-1=8,why the answer it give me is 6

    test no.170
    Input: [1,2,4,2,5,7,2,4,9,0]
    Output: 15
    Expected: 13
    my idea is res =4-1 + 7-2 +9-4=15,why the answer it give me is 13

    code:
    int maxProfit(vector<int>& prices) {

        vector<int>  mid(prices.size(),0);
        vector<vector<int> > DP(prices.size(),mid);
        
        for(int i=0;i<DP.size();i++)
        for(int j=i+1;j<prices.size();j++)
        {
            DP[i][j]=prices[j]-prices[i];
        }
        int res =0;
        
        for(int k=0;k<DP.size();k++)
        {
            int mid_res=0;
            int flag =false;
            for(int i=k;i<DP.size();i++)
            {
                for(int j=i+1;j<prices.size();j++)
                {
                    if(DP[i][j-1]>DP[i][j])
                    {
                        mid_res+=DP[i][j-1];//max(res,DP[i][j-1]);
                        i=j-1;
                        break;
                    }
                    else if(prices.size()-1==j) {mid_res+=DP[i][j];flag=true;}
                }
                if(flag)break;
            }
            
            
            res=max(res,mid_res);
        }
        return res;
        
    }

  • 0
    Y

    sorry ,i am not read the examination question take to heart,sorry.


Log in to reply
 

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