Cutting Boards

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