WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Dynamic Programming Β· Advanced Β· question 43 of 100

Solve the Weighted Job Scheduling problem using dynamic programming.?

πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

The Weighted Job Scheduling problem is a common problem in computer science where we have N jobs, each with a start and end time, and a weight (profit) associated with completing the job. The objective is to find the maximum profit subset of mutually compatible jobs, where the jobs are compatible if they do not overlap in time.

We can solve this problem using dynamic programming by defining a subproblem and building a solution for the larger problem using the solutions to the subproblems. Let’s write an algorithm and code in Java to solve this problem.

Algorithm:

1. Sort the jobs by their end times in non-decreasing order so that we can easily find the compatible jobs.

2. For each i (0<=i<N), let DP[i] be the maximum profit we can obtain by scheduling the jobs from 0 to i.

3. DP[0] = jobs[0].weight

4. For i from 1 to N-1, find the largest index j (0<=j<i) such that jobs[j] does not overlap with jobs[i] and set DP[i] = max(j, DP[j] + jobs[i].weight).

5. Return DP[N-1] as the maximum profit.

Code:

Here, we assume that we have a class "Job" with start and end times and weight attributes.

public int weightedJobScheduling(Job[] jobs) {
    // Step 1: sort the jobs by their end times
    Arrays.sort(jobs, (a, b) -> a.end - b.end);
    
    // Step 2: define DP array
    int N = jobs.length;
    int[] DP = new int[N];
    
    // Step 3: initialize DP[0]
    DP[0] = jobs[0].weight;
    
    // Step 4: fill DP array
    for (int i = 1; i < N; i++) {
        DP[i] = jobs[i].weight;
        for (int j = 0; j < i; j++) {
            if (jobs[j].end <= jobs[i].start) {
                DP[i] = Math.max(DP[i], DP[j] + jobs[i].weight);
            }
        }
    }
    
    // Step 5: return DP[N-1]
    return DP[N-1];
}

Example:

Suppose we have the following jobs:

| Job ID | Start Time | End Time | Weight |
|--------|------------|----------|--------|
|   J1   |      1     |     3    |   5    |
|   J2   |      2     |     5    |   6    |
|   J3   |      4     |     6    |   5    |
|   J4   |      6     |     7    |   4    |
|   J5   |      5     |     8    |   11   |
|   J6   |      7     |     9    |   2    |

Applying the algorithm in the previous code, we obtain DP = [5, 6, 6, 10, 16, 16]. The maximum profit is 16, which is obtained by selecting jobs J1, J2, and J5.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview β€” then scores it.
πŸ“ž Practice Dynamic Programming β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

All 100 Dynamic Programming questions Β· All topics