Cutting Boards

Sort by

recency

|

191 Discussions

|

  • + 1 comment

    I don't understand the task. I'm already having problems understanding what the input numbers are supposed to represent.

    1
    2 2
    2
    1
    

    what is the 1? 1x1 ? what is 2 2 = 2x2? what's the deal with modulo? don't understand this wild calculation

    but it gets even crazier

    1
    6 4
    2 1 3 1 4
    4 1 2
    

    what are 2 1 3 1 4 and 4 1 2 supposed to be anyway?

  • + 0 comments
    def boardCutting(cost_y, cost_x):
        MOD = 10**9 + 7
        
        # Sort costs in descending order
        cost_y.sort(reverse=True)
        cost_x.sort(reverse=True)
        
        # Variables to track the number of segments
        horizontal_segments = 1
        vertical_segments = 1
        
        total_cost = 0
        
        i, j = 0, 0
        while i < len(cost_y) and j < len(cost_x):
            if cost_y[i] >= cost_x[j]:
                total_cost += cost_y[i] * vertical_segments
                horizontal_segments += 1
                i += 1
            else:
                total_cost += cost_x[j] * horizontal_segments
                vertical_segments += 1
                j += 1
            total_cost %= MOD
        
        # If there are remaining horizontal cuts
        while i < len(cost_y):
            total_cost += cost_y[i] * vertical_segments
            horizontal_segments += 1
            i += 1
            total_cost %= MOD
        
        # If there are remaining vertical cuts
        while j < len(cost_x):
            total_cost += cost_x[j] * horizontal_segments
            vertical_segments += 1
            j += 1
            total_cost %= MOD
        
        return total_cost
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'boardCutting' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    #  1. INTEGER_ARRAY cost_y
    #  2. INTEGER_ARRAY cost_x
    #
    
    def boardCutting(cost_y, cost_x):
        MOD = 10**9 + 7
        
        # Combine the cost arrays and sort them
        all_costs = sorted([(cost, 'h') for cost in cost_y] + [(cost, 'v') for cost in cost_x], reverse=True)
        
        # Initialize variables
        horizontal_segments = 1
        vertical_segments = 1
        total_cost = 0
        
        # Process each cut in sorted order
        for cost, cut_type in all_costs:
            if cut_type == 'h':
                total_cost = (total_cost + cost * vertical_segments) % MOD
                horizontal_segments += 1
            elif cut_type == 'v':
                total_cost = (total_cost + cost * horizontal_segments) % MOD
                vertical_segments += 1
        
        return total_cost
    
    if __name__ == '__main__':
    
        q = int(input().strip())
    
        for q_itr in range(q):
            first_multiple_input = input().rstrip().split()
    
            m = int(first_multiple_input[0])
    
            n = int(first_multiple_input[1])
    
            cost_y = list(map(int, input().rstrip().split()))
    
            cost_x = list(map(int, input().rstrip().split()))
    
            result = boardCutting(cost_y, cost_x)
            print(result)
    
  • + 0 comments
    int boardCutting(vector<int> cost_y, vector<int> cost_x) {
        long h = 1, v = 1;
        sort(cost_x.rbegin(), cost_x.rend());
        sort(cost_y.rbegin(), cost_y.rend());
        
        int s1 = 0, s2 = 0;
        long res = 0;
        while (s1 < cost_x.size() && s2 < cost_y.size()) {
            if (cost_x[s1] > cost_y[s2]) {
                res += cost_x[s1] * v;
                h++;
                s1++;
            } else {
                res += cost_y[s2] * h;
                v++;
                s2++;
            }
            res %= 1000000007;
            
        }
        
        while (s1 < cost_x.size()) {
            res += cost_x[s1] * v;
            res %= 1000000007;
            
            s1++;
        }
        
        while (s2 < cost_y.size()) {
            res += cost_y[s2] * h;
            res %= 1000000007;
            s2++;
        }
        
        return (int)(res % 1000000007);
    }
    
  • + 0 comments
    public static int boardCutting(List<Integer> cost_y, List<Integer> cost_x) {
    // Write your code here
    long minCost = 0;
    Collections.sort(cost_y, Comparator.reverseOrder());
    Collections.sort(cost_x, Comparator.reverseOrder());
    long hp = 1;
    long vp = 1;
    int hi = 0;
    int vi = 0;
    while(hi < cost_y.size() && vi < cost_x.size()){
        if(cost_y.get(hi) > cost_x.get(vi)){
            minCost = minCost + vp*cost_y.get(hi);
            hp++;
            hi++;
        } else {
            minCost = minCost + hp*cost_x.get(vi);
            vi++;
            vp++;
        }
    }
    while(hi < cost_y.size()){
        minCost = minCost + vp*cost_y.get(hi);
        hi++;
    }
    while(vi< cost_x.size()){
         minCost = minCost + hp*cost_x.get(vi);
        vi++;
    }
    
    return (int)(minCost%1000000007);
    }