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

Dynamic Programming Β· Guru Β· question 92 of 100

Solve the problem of finding the Longest Increasing Subsequence with a Specific Cost Function using dynamic programming.?

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

The Longest Increasing Subsequence problem involves finding the longest subsequence of a given array of numbers, where the subsequence is in increasing order. However, in this variant of the problem, we have a specific cost function associated with each element of the sequence, which we need to take into account while finding the longest increasing subsequence.

To solve this problem using dynamic programming, we can use the following approach:

1. Create an array β€˜dpβ€˜ of size β€˜nβ€˜, where β€˜nβ€˜ is the length of the original sequence. β€˜dp[i]β€˜ will store the length of the longest increasing subsequence ending at index β€˜iβ€˜.

2. Initialize β€˜dp[i]β€˜ to β€˜1β€˜ for all β€˜iβ€˜ in β€˜[0, n-1]β€˜, as any element can form a subsequence of length 1.

3. For each index β€˜iβ€˜ in β€˜[1, n-1]β€˜, iterate over all indices β€˜jβ€˜ in β€˜[0, i-1]β€˜. If β€˜a[j] < a[i]β€˜ and β€˜cost[j] < cost[i]β€˜ (where β€˜aβ€˜ is the original sequence and β€˜costβ€˜ is the array containing the cost function values), then update β€˜dp[i]β€˜ as β€˜max(dp[j] + 1, dp[i])β€˜. This means that if we can extend the longest increasing subsequence ending at index β€˜jβ€˜ with the element at index β€˜iβ€˜, and the cost of the subsequence doesn’t increase, then we should update the length of the longest increasing subsequence ending at index β€˜iβ€˜.

4. Finally, the solution to the problem is the maximum value in the β€˜dpβ€˜ array.

Here’s the Java code for this approach:

public int longestIncreasingSubsequenceWithCost(int[] a, int[] cost) {
    int n = a.length;
    int[] dp = new int[n];
    // Initialize dp array to 1 as any element can form a subsequence of length 1.
    Arrays.fill(dp, 1);
    // Iterate over all indices i and j to update dp array.
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (a[j] < a[i] && cost[j] < cost[i]) {
                dp[i] = Math.max(dp[j] + 1, dp[i]);
            }
        }
    }
    // Find the maximum value in the dp array, which is the length of the longest increasing subsequence with the given cost function.
    int maxLength = 0;
    for (int i = 0; i < n; i++) {
        maxLength = Math.max(maxLength, dp[i]);
    }
    return maxLength;
}

Let’s take an example to see how this algorithm works. Consider the sequence β€˜[4, 2, 8, 5, 10]β€˜ and the cost function β€˜[1, 4, 2, 3, 7]β€˜. We want to find the longest increasing subsequence with the given cost function.

The β€˜dpβ€˜ array will be initialized as β€˜[1, 1, 1, 1, 1]β€˜. We then iterate over all possible β€˜(i, j)β€˜ pairs to update the β€˜dpβ€˜ array:

- When `i = 1` and `j = 0`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `2`.
- When `i = 2` and `j = 0`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `2`.
- When `i = 2` and `j = 1`, we have `a[j] > a[i]`, so we skip this pair.
- When `i = 3` and `j = 0`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `2`.
- When `i = 3` and `j = 1`, we have `a[j] > a[i]`, so we skip this pair.
- When `i = 3` and `j = 2`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `3`.
- When `i = 4` and `j = 0`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `2`.
- When `i = 4` and `j = 1`, we have `a[j] > a[i]`, so we skip this pair.
- When `i = 4` and `j = 2`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `3`.
- When `i = 4` and `j = 3`, we have `a[j] < a[i]` and `cost[j] < cost[i]`, so we update `dp[i]` as `max(dp[j] + 1, dp[i])`, which becomes `4`.

The final β€˜dpβ€˜ array becomes β€˜[1, 2, 2, 3, 4]β€˜, and the solution to the problem is the maximum value in this array, which is β€˜4β€˜. Therefore, the longest increasing subsequence with the given cost function is β€˜[4, 8, 10]β€˜.

Overall, the time complexity of this algorithm is β€˜O(n2)β€˜, as we have two nested loops to iterate over all possible β€˜(i, j)β€˜ pairs. However, this solution can be optimized to β€˜O(nlogn)β€˜ using binary search instead of nested loops.

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