Cutting Boards

Sort by

recency

|

192 Discussions

|

  • + 0 comments

    For JS, remember to use BigInt, otherwise, test case 11 will be failed.

    function boardCutting(cost_y, cost_x) {
        // Write your code here
        let sum =BigInt(0);
        let sortedCostX = [...cost_x].toSorted((a,b) => b-a);
        let sortedCostY = [...cost_y].toSorted((a,b) => b-a);
        let px = 0, py = 0;
        while(px < sortedCostX.length && py < sortedCostY.length) {
            if(sortedCostX[px] > sortedCostY[py]) {
                sum += BigInt(py+1) * BigInt(sortedCostX[px]);
                px++;
            } else {
                sum += BigInt(px+1) * BigInt(sortedCostY[py]);
                py++;           
            }
        }
        while(px < sortedCostX.length) {
            sum += BigInt(py+1) * BigInt(sortedCostX[px]);
            px++;
        }    
        while(py < sortedCostY.length) {
            sum += BigInt(px+1) * BigInt(sortedCostY[py]);
            py++;
        }
        return sum % BigInt(10**9+7);
    }
    
  • + 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

      The ask is to cut the board in 1 X 1 pieces and there are horizontal and vertical markings present on the board that will help the person to cut them in 1 X 1 size of smaller boards and those markings have some cost associated with them. Now the task is to choose the sequence that cost you the minimum. Keep in mind the segments- check the Sample I/O example with explaination.

      • 1 # No. of test cases
      • 2 2 # Size of the actual board you are given, N X M
      • 2 # Cost(s) of cutting the markings on Y-axis
      • 1 # Cost(s) of cutting the markings on X-axis

      The board is 2 X 2 and you have to cut it in 1 X 1 pieces.

  • + 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);
    }