It is wrong in only some cases,help!


  • -1
    X

    if i enterInput: 2, [3,2,6,5,0,3]
    Output: 4
    Expected: 7

    int maxProfit(int k, int prices[], int n) {
    int profit[n],profit1[n];
    int i,l,mon=0,j=0;
    int own=0,temp=0;
    if(n<=1) own=0;
    else
    {
    profit[0]=0;
    for(i=0;(i+1)<n;i++)
    {
    if(prices[i]<=prices[i+1])
    {
    mon=prices[i+1]-prices[i]+mon;
    profit[j]=mon;
    }
    else
    {
    j++;
    mon=0;
    profit[j]=mon;
    }
    }
    if(j<=k)
    {
    for(l=0;l<=j;l++)
    own+=profit[l];
    }
    else
    {
    for(l=0;l<j;l++)
    {
    for(j=j-1;j>=i;j--)
    {
    if(profit[j]>profit[j-1])
    {
    temp=profit[j];
    profit[j]=profit[j-1];
    profit[j-1]=temp;
    }
    }
    }
    for(j=0;j<k;j++)
    own+=profit[j];
    }
    }
    return own;
    }


Log in to reply
 

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