You’re the owner of a concert hall. There are a list of performances $[1..n]$ with start time $s_{i}$, end time $t_{i}$ and price $p_{i}$. We want to pick the set of performances to rent out such that we can collect the maximum amount of rent collected.

This problem can be solved with DP. We consider the following properties:

- The solution space $S$ is given by all the subsets of performances that occur
- The function $f$ says that for each $s∈S$, we have $f(s)=∑_{i∈s}p_{i}$.
- The constraints $K$ here is that performances cannot overlap.

Problem

Input:tuples $(s_{1},t_{1},p_{1}),…,(s_{n},t_{n},p_{n})$.

Output:a subset $s⊂[n]$ such that the performances don’t overlap, and we get the maximum $∑_{i∈s}p_{i}$.

## Naive solution

$S$ are all the subsets of performances, which is given by $∣S∣=2_{n}$ (the power set). We could iterate through each one and calculate the collected rent for each. But this has an exponential runtime.

## Potential high level questions

- We’re maximizing total rent collected, we should start with performances with the highest prices
- Performances can’t overlap, maybe we should keep track of the max possible money to be made in a given time period
- Sort the performances by price so we can consider the higher prices first
- Sort w.r.t. time so that we can easily check in linear time whether performances overlap

## Some intuition

Let’s say that we process inputs by the starting times $s_{i}$, so $s_{1}<s_{2}<⋯<s_{n}$. The question we should ask, is, do I rent out to performance $p_{1}$?

- If yes, then we find the first performer where $s_{j(1)}≥t_{1}$, because anyone before the first performer’s end time cannot be booked. Then we can say the total reward is given by $p_{1}+ALG([j(1)..n])$, as we’re considering the rest of the performers recursively.
- If no, then we total reward is given by $ALG([2..n])$. We don’t discard performance 1, we just don’t consider it, and recurse on the rest of the performances.

## Greedily solving without caching

Similar to divide and conquer, we recurse into both options (yes or no), and then calculate the max reward we can get if we choose it. We then compare which one returns the higher price, $C_{yes}$ or $C_{no}$, and choose that instead.

The base case is when the array only has one element (some performance $i$), in which case we rent it out to them and return $p_{i}$.

- Find $j(1)$, the successor to current first performance in the list.
- Run the algorithm on array $[j(1)..n]$. return $C_{yes}$.
- Run the algorithm on array $[2..n]$. return $C_{no}$.
- Output $max(C_{yes}+p_{1},C_{no})$.

### Runtime

We can use binary search to find $j(1)$, which runs in $O(gn)$. Then, we are recursing onto an array of size $n−j(1)$ (if we chose yes), as well as an array of size $n−1$ (if we chose no). The recurrence is then given by

$T(n)=O(gn)+T(n−j(1))+T(n−1)$Which must be greater than or equal to

$T(n)=O(1)+2T(n−1)$Worst case, $j(1)=1$. We decrement $n$ by 1 each time so we will recurse $O(n)$ times, which means we multiply by $2$ a total of $n$ times. Therefore runtime is $2_{n}$. This… isn’t any better than naive.

## Memoizing the previous algorithm

Using divide and conquer repeats calls to the same subarrays and so we waste time computing the same things over and over.

Let us *cache values that we’ve computed*, and whenever we’re about to recurse we first check if we’ve already computed it before. Before we execute a recursive call, we check if we’ve run it already.

Let $G[1..n]$ be an array that stores our computed values. If a $G[i]$ is non-null, this means we’ve already computed the max reward for $[i..n]$. More formally, we can say

$G[i]=max(p_{i}+G[j(1)],G[i+1])$- Find $j(1)$ using binary search.
- Check if $G[j(1)]$ is non-null. If yes, then use that value. If not, then we actually recurse, and store the value $C_{yes}$ into $G[j(1)]$.
- Do the same thing for $i=2$. check $G[2]$, if it’s non-null use the stored value, if it’s null then we recurse into $[2..n]$ to calculate the value $C_{no}$ and store in $G[2]$.
- Output $max(p_{i}+C_{yes},C_{no})$.

## Runtime

We do binary search $O(gn)$ in each subproblem, where there are $n$ of. Therefore, the runtime is $O(ngn)$.