Rod Cutting

There is a rod of length N lying on x-axis with its left end at x = 0 and right end at x = N. Now, there are M weak points on this rod denoted by positive integer values(all less than N) A1, A2, …, AM. You have to cut rod at all these weak points. You can perform these cuts in any order. After a cut, rod gets divided into two smaller sub-rods. Cost of making a cut is the length of the sub-rod in which you are making a cut.

Your aim is to minimise this cost. Return an array denoting the sequence in which you will make cuts. If two different sequences of cuts give same cost, return the lexicographically smallest.

Notes:

  • Sequence a1, a2 ,…, an is lexicographically smaller than b1, b2 ,…, bm, if and only if at the first i where ai and bi differ, ai < bi, or if no such i found, then n < m.

  • N can be upto 109.

For example,

N = 6
A = [1, 2, 5]

If we make cuts in order [1, 2, 5], let us see what total cost would be.
For first cut, the length of rod is 6.
For second cut, the length of sub-rod in which we are making cut is 5(since we already have made a cut at 1).
For third cut, the length of sub-rod in which we are making cut is 4(since we already have made a cut at 2).
So, total cost is 6 + 5 + 4.

Cut order          | Sum of cost
(lexicographically | of each cut
 sorted)           |
___________________|_______________
[1, 2, 5]          | 6 + 5 + 4 = 15
[1, 5, 2]          | 6 + 5 + 4 = 15
[2, 1, 5]          | 6 + 2 + 4 = 12
[2, 5, 1]          | 6 + 4 + 2 = 12
[5, 1, 2]          | 6 + 5 + 4 = 15
[5, 2, 1]          | 6 + 5 + 2 = 13


So, we return [2, 1, 5].
public class Solution {
    ArrayList<Integer> result; // Stores the result
    int[] cuts; // Stores -> Start, Cuts & End
    int[][] cut; // Stores the cut which will give us the min cost between start & end.

    public ArrayList<Integer> rodCut(int rod, ArrayList<Integer> scores) {
        int n = scores.size() + 2;
        // Creating cuts array
        cuts = new int[n];
        // Start
        cuts[0] = 0;
        // Cuts
        for (int i = 0; i < scores.size(); i++)
            cuts[i + 1] = scores.get(i);
        // End
        cuts[n - 1] = rod;
        // dp[s][e] -> Minimum cost between s & e.
        long[][] dp = new long[n][n];
        cut = new int[n][n];
        // Base cases, when start == end (rod length = 0), then ans=0
        // Because base cases form a diagonal, we will use our usual way
        for (int start = n - 1; start >= 0; start--) {
            for (int end = start + 1; end < n; end++) {
                // Considering all the points between start & end, as the first cut
                for (int k = start + 1; k < end; k++) {
                    // cuts[end] - cuts[start] -> cost for any cut between start & end.
                    long sum = cuts[end] - cuts[start] + dp[start][k] + dp[k][end];
                    if (dp[start][end] == 0 || sum < dp[start][end]) {
                        dp[start][end] = sum;
                        cut[start][end] = k;
                    }
                }
            }
        }
        result = new ArrayList<>();
        backTrack(0, n - 1);
        return result;
    }

    private void backTrack(int s, int e) {
        if (s + 1 >= e)
            return;
        result.add(cuts[cut[s][e]]);
        backTrack(s, cut[s][e]);
        backTrack(cut[s][e], e);
    }
}

Last updated