Cutting Boards

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