Skip to main content
More explanation.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

The most common subproblems where a particular subsequence should be selected are parametrized by the index of the last element selected. The classical, simple and brilliant example is Kadane's algorithm.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$$1\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

TheWe could add an artificial base case is all, $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented. 

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

The most common subproblems where a particular subsequence should be selected are parametrized by the index of the last element selected. The classical, simple and brilliant example is Kadane's algorithm.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

The base case is all $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented.

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

The most common subproblems where a particular subsequence should be selected are parametrized by the index of the last element selected. The classical, simple and brilliant example is Kadane's algorithm.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $1\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

We could add an artificial base case, $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented. 

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

More explanation.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

I am afraid that theThe most common subproblems where a particular subsequence should be selected are rather obviousparametrized by the index of the last element selected. The classical, simple and brilliant example is Kadane's algorithm.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

The base case is all $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented.

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

I am afraid that the subproblems are rather obvious.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

The base case is all $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented.

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

The most common subproblems where a particular subsequence should be selected are parametrized by the index of the last element selected. The classical, simple and brilliant example is Kadane's algorithm.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

The base case is all $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented.

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.

Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

I am afraid that the subproblems are rather obvious.

Let the given intervals are $I_1, I_2,\cdots, I_n$, where $I_j=[l_j, r_j]$.

The subproblem $DP[i]$, where $0\le i\le n$ is the maximum rent if the last interval rented is the $i$-th interval. The answer is $\max_iDP[i]$.

The base case is all $DP[0]=0$, which says that the maximum rent is 0 if there is no interval rented.

The recurrence relation is $$DP[i] = \text{Length}(I_i) + \max_{r_j\lt l_i} DP[j].$$

That is it, basically. There is a minor problem with the above recurrence relation, however. When we compute $DP[i]$, not all of $DP[j]$ with $r_j\lt l_i$ has been computed. I will leave it for you to resolve this minor obstacle.