Split a Long Stay Across Hotels With Minimum-Night Rules
Last updated: November 29, 2026
Quick Overview
A traveler needs m consecutive nights and can choose among n hotels, where price[h][d] is hotel h's rate for night d and each hotel requires at least min_nights[h] consecutive nights per stay; every hotel change costs a fixed switch_fee. Return the minimum total cost to cover all nights, or -1 if no valid plan exists.
Agoda
November 29, 20260
0
0 solved
A traveler needs m consecutive nights and can choose among n hotels, where price[h][d] is hotel h's rate for night d and each hotel requires at least min_nights[h] consecutive nights per stay; every hotel change costs a fixed switch_fee. Return the minimum total cost to cover all nights, or -1 if no valid plan exists.
Agoda shows nightly rates that vary by date and enforces hotel length-of-stay restrictions, so a "split stay" recommender is a natural product hook. Onsite coding rounds use this to see whether you can define DP state precisely and then remove a hidden inner loop to reach O(n * m).
What the Interviewer Expects
- Define state as the cheapest cost to cover nights [0, d) with a block ending exactly at d
- Use prefix sums so any block cost is O(1)
- Write the naive O(n * m^2) transition, then optimize with a running minimum per hotel
- Argue why consecutive blocks at the same hotel never beat merging them, so the fee rule stays simple
- Handle infeasible inputs and return -1
- Validate against a brute force on small random cases
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you reconstruct the actual plan (which hotel on which nights)?
- What if the switch fee depends on the pair of hotels, such as distance between them?
- Add a maximum-nights rule per hotel as well; how does the running minimum change? (Hint: sliding window minimum.)
- Limit the plan to at most k switches; what is the new state and complexity?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Nights are indexed 0 to m-1. A plan is a sequence of contiguous blocks covering all nights, each block at one hotel with length at least min_nights[h]. Cost is the sum of nightly prices plus `switch...
High-Level Design
Let end[d] be the cheapest cost to cover nights [0, d) where a block ends exactly at d, with end[0] = 0. A block at hotel h over [s, d) gives `end[d] = min(end[s] + fee(s) + P[h][d] - P[h][s])...