Sort by

recency

|

262 Discussions

|

  • + 0 comments

    Annoyingly ... we hade to update the main function to get the code to loop through all test cases ... shoulda put that in the instructions.

  • + 0 comments

    Python: you need to FIX THE MAIN!

    It should be:

    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input().strip())
    
        for _ in range(0, t):
            first_multiple_input = input().rstrip().split()
    
            n = int(first_multiple_input[0])
    
            k = int(first_multiple_input[1])
    
            arr = list(map(int, input().rstrip().split()))
        
            result = unboundedKnapsack(k, arr)
    
            fptr.write(str(result) + '\n')
    
        fptr.close()
    

    Resolution:

    def unboundedKnapsack(k, arr):
        # progressively get responses (DP)
        # k 0 1 2 3 4 5 6
        # 0 0 0 0 0 0 0 0 
        # 2 0 0 2 2 4 4 6    dp[i][j] = max(dp[i-1][j], dp[i][j-i]+i)
        # 3 0 0 2 3 4 5 6    (choosing between not adding anything and
        # 4 0 0 2 3 4 5 6     adding the current N's value to the Nth
        #                     value behind)
        
        # stop searching if k is found. If not, return largest.
        
        # Creating dp table
        dp = [[0 for _ in range(0, k+1)] for _ in range(0, len(arr)+1)]
        options = [0] + arr
        num_ints = len(arr)
        max_val = 0
    
        for i in range(1, num_ints+1):
            for j in range(1, k+1):
                if j < options[i]:
                    # is the current k less than the value of the line?
                    dp[i][j] = dp[i-1][j]
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-options[i]]+options[i])
                if dp[i][j] == k:
                    # found k in DP table
                    return k
                max_val = max(max_val, dp[i][j])
                j += 1
            i += 1
            
        # k is not on DP table, so returning max value found
        return max_val
    
  • + 0 comments

    solution using C with recursion function. What I did here is to try to get every possible combination in an optimal way because there is no other way to know the highest possible sum because there are no limits on the numbers used to get this sum. So for each element in the array, start from the first index and get the maximum sum using it. Store this sum, and each time, reduce this sum by the value in the specific index. and pass this sum to the next index. and do it recursively for all indexes.

    for main function issues, just do a for loop t times from char** first_multiple_input = split_string(rtrim(readline())); until fprintf(fptr, "%d\n", result);

    the code :

    int cumulative_sum(int* arr , int arr_count ,int  k ,int index , int sum)
    {
        if(index == arr_count)
        {
            return sum;
        }
        else if( k%arr[index] == 0)
        {
            return k;
        }
        int max_sum = 0 , temp = 0 , count = 0 ,  i = 0;
        while (sum <= k) {
            sum += arr[index];
            count++;
        }
        sum -= arr[index];
        max_sum = sum;
        
        for (i = 0 ; i< count ; i++) {
            temp = cumulative_sum(arr , arr_count , k , index+1 , sum);
            if(temp == k)
            {
                return k;
            }
            else if(temp > max_sum)
            {
                max_sum = temp;
            }
            sum -= arr[index];
        }
        
        return max_sum;
    }
    
    /*
     * Complete the 'unboundedKnapsack' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY arr
     */
    
    int unboundedKnapsack(int k, int arr_count, int* arr) {
    
        return cumulative_sum(arr,arr_count,k,0,0);
    
    }
    
  • + 0 comments

    I dont like such questions and platforms where I need to update my main() function as well to pass multiple test cases. Here I was waiting and searching error in my solution that why only one test case result is getting print, only to figure out I had to myslef run loop for all t values.

  • + 0 comments

    Fraction Knapsack

    include

    include

    include

    include

    include

    using namespace std;

    bool com (const vector& a,const vector& b) { return a[2]>b[2]; } int main() { int n =0; cin>>n;

    vector<vector<double>> a(n, vector<double> (4));
    int sol[n]={0};
        double profit=0,w;
    
     /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    for(int i=0;i<n;i++)
    {
        cin>>a[i][0];
        cin>>a[i][1];
        a[i][2]= a[i][0]/a[i][1];
        a[i][3]=i;
    }
    

    cin>>w;

    sort(a.begin(),a.end(),com);
    
    int  i=0;
    double max=0;
         for(i=0;i<n;i++)
       {
    
        if(w>=max+a[i][1])
          {
    
            max+=a[i][1];
            sol[(int)a[i][3]]++;
            profit+=a[i][0];
          }
             else
             {
                 break;
             }
       }
      if(w>max)
      {
           sol[(int)a[i][3]]++;
           profit+=(w-max)*a[i][2];
      }
    
    cout<<(int)profit<<endl;
    for(int i=0;i<n;i++)
    {
         cout<<sol[i]<<" ";
    }
    
    
    
    return 0;
    

    }