dsa1 min read
House Robber II — Circular Array DP
Houses arranged in a circle: first and last are adjacent. Run linear House Robber twice: once excluding last house, once excluding first.
Read →
webcoderspeed.com
1276 articles
Houses arranged in a circle: first and last are adjacent. Run linear House Robber twice: once excluding last house, once excluding first.
Picking value k deletes all k-1 and k+1 elements. Map to House Robber: earn[k] = k * count(k), then max non-adjacent sum.
Find the contiguous subarray with the largest sum. Kadane's algorithm: either extend previous subarray or start fresh at current element.
Find maximum product of a contiguous subarray. Track both min and max at each position because a negative * negative becomes positive.
Find minimum coins to make amount. Unbounded knapsack DP: dp[amount] = min(dp[amount-coin]+1) over all coins. Bottom-up from 0 to amount.