• + 0 comments

    Solution in C

    int poisonousPlants(int p_count, int* p) {
         int rounds=0;
    
        int* tstack=(int*)malloc(p_count * sizeof(int));
        int tstack_cnt=0;
        tstack[0]=p[0];
    
        for (;;)
        {    
            tstack_cnt=1;
    
            for (int i=0;i<p_count-1;i++)
                if (p[i]>=p[i+1])
                    tstack[tstack_cnt++]=p[i+1];
                
            if (tstack_cnt==p_count)
                return rounds;
            rounds++;
            memcpy(p,tstack,tstack_cnt*sizeof(int));
            p_count=tstack_cnt;
        }
    
    
    }