Cutting Boards

  • + 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