spiralgo / algorithms

We voluntarily solve algorithm problems on this repo.
24 stars 7 forks source link

198. House Robber & 213. House Robber II #359

Closed ErdemT09 closed 3 years ago

ErdemT09 commented 3 years ago

198. House Robber

Here, the maximum robbed money at every house would equal to the maximum of: the amount stolen at the previous house, the amount stolen at the house before + the current house's money. We can hence turn this problem into a dynamic programming problem, where the result is the amount stolen at the last house.

213. House Robber II

As the first and the last houses are connected, the maximum sum equals the maximum of the maximum sum of the houses without the first or without the last. We apply the algorithm from the first problem in two passes and compare the results.