Leetcode Problem 2830. Maximize the Profit as the Salesman

2830. Maximize the Profit as the Salesman

Leetcode Solutions

Dynamic Programming with Binary Search

  1. Sort the offers based on their starting times, and if they are the same, then based on their ending times.
  2. Initialize a memoization array dp with -1 to indicate that no value has been computed yet.
  3. Define a recursive function getProfit that takes the current index and returns the maximum profit from that index onwards.
  4. In getProfit, if the current index is out of bounds, return 0.
  5. If the value in dp at the current index is not -1, return it as the maximum profit has already been computed.
  6. Compute the profit by not selling the current offer and moving to the next index.
  7. Use binary search to find the next non-conflicting offer and compute the profit by selling the current offer and adding it to the profit from the next non-conflicting offer.
  8. Store the maximum of these two profits in dp at the current index.
  9. Return the maximum profit from the first offer by calling getProfit(offers, offers.size(), 0).
UML Thumbnail

Iterative Dynamic Programming

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...