Your code might be failing for larger test-cases fail


#1
  • std::vector solve(int A, std::vector& B) {
  • if (B.size()<2)
    
  • {
    
  •     std::vector<int>ans(A / B[0], B[0]);
    
  •     return ans;
    
  • }
    
  • int maxhits = 0; //maximum count of hits 
    
  • int minVal=INT_MAX;//minimum value
    
  • int MinIndex = 0;//minimum value index
    
  • for (size_t i = 0; i < B.size(); i++) //find minimum value and count  maximum count of hits 
    
  • {
    
  •     if (minVal >B[i])
    
  •     {
    
  •         minVal = B[i];
    
  •         maxhits = A / B[i];
    
  •         MinIndex = i;
    
  •     }
    
  • }
    
  • std::vector<int>ans(maxhits); // create and allocate vector for hits with maxhits number of members
    
  • int rem = A % B[MinIndex]; //remainder (if rem=1 means minValue can be replaced by 
     B[i]=minValue+1 if encountered before)
    
  • int RepCount = 0;
    
  • int ansI = 0;//index of ans
    
  • if (rem)
    
  • {
    
  •     for (size_t i = 0; i < MinIndex; i++)//for any from 0 till minimum value
    
  •     {
    
  •         if (B[i]-B[MinIndex]<=rem) //case where minimum value can be replaced by B[i]
    
  •         {
    
  •             RepCount = rem / (B[i] - B[MinIndex]); //replace as mush as possible
    
  •             RepCount = RepCount > maxhits ? maxhits : RepCount; 
    
  •             for (size_t j = 1; j <= RepCount; j++)//push i to ans
    
  •             {
    
  •                 ans[ansI]=i;
    
  •                 ansI++;
    
  •             }
    
  •             maxhits -= RepCount;//update maxhits
    
  •             rem -= (B[i] - B[MinIndex])* RepCount; //update remainder
    
  •             RepCount = 0;
    
  •         }
    
  •         if (!rem||!maxhits)
    
  •         {
    
  •             break;
    
  •         }
    
  •     }
    
  • }
    
  • while (maxhits)//push minimum value up to what's left of maxhits
    
  • {
    
  •     ans[ansI]=MinIndex;
    
  •     ansI++;
    
  •     maxhits--;
    
  • }
    
  • return ans;
    
  • }
    Any idea why greedy Q(n) time and Q(1) space should fail because of " Your code might be failing for larger test-cases" ?`

* 1. Preformatted text

`