Imagine you're a census taker walking through a mysterious circular neighborhood where you need to count the total number of houses. You start at a random house and can only interact with the street through a limited Street API.
The street forms a perfect circle - if you keep walking in one direction, you'll eventually return to where you started. Each house has a door that can be either open or closed, and you have the power to manipulate these doors as you move around.
Your Mission: Determine the exact number of houses on this circular street using the minimal number of API calls.
Available Street API Methods:
openDoor()- Open the current house's doorcloseDoor()- Close the current house's doorisDoorOpen()- Check if current door is open (returns boolean)moveRight()- Move to the next house clockwisemoveLeft()- Move to the next house counter-clockwise
You know there are at most k houses on the street, but the exact count is what you need to discover. The challenge is to do this efficiently while working with an interactive environment.
Input & Output
Visualization
Time & Space Complexity
Single pass around the circle, visiting each house exactly once
Only need a counter variable, no additional data structures
Constraints
- 1 โค number of houses โค k
- k โค 103
- Each house door is initially either open or closed
- The street forms a perfect circle
- You start at an arbitrary house position