Count Houses in a Circular Street II - Problem
Count Houses in a Circular Street II
Imagine you're a census worker visiting a circular street where houses are arranged in a perfect circle. Each house has a door that can be either open or closed, and at least one door is guaranteed to be open initially.
You start in front of one house and need to count the total number of houses on the street. The challenge? You can only move clockwise and have no idea where you started or how many houses there are (though you know it's at most
Available Operations:
•
•
•
Since the street is circular, after house
Example: If there are 4 houses arranged as [Open, Closed, Open, Closed], you need to return
Imagine you're a census worker visiting a circular street where houses are arranged in a perfect circle. Each house has a door that can be either open or closed, and at least one door is guaranteed to be open initially.
You start in front of one house and need to count the total number of houses on the street. The challenge? You can only move clockwise and have no idea where you started or how many houses there are (though you know it's at most
k houses).Available Operations:
•
isDoorOpen() - Check if current house door is open•
closeDoor() - Close the current house door•
moveRight() - Move clockwise to the next houseSince the street is circular, after house
n comes house 1 again. Your goal is to determine the exact number of houses without getting stuck in an infinite loop!Example: If there are 4 houses arranged as [Open, Closed, Open, Closed], you need to return
4. Input & Output
example_1.py — Basic Circle
$
Input:
street = [Open, Closed, Open, Closed], k = 10
Starting at house 0
›
Output:
4
💡 Note:
There are 4 houses in total. We start at house 0 (open), close it, then move through houses 1(closed), 2(open), 3(closed) and back to house 0 (now closed), counting 4 houses total.
example_2.py — All Doors Open
$
Input:
street = [Open, Open, Open], k = 10
Starting at house 1
›
Output:
3
💡 Note:
All 3 doors start open. We close door at house 1, move to house 2 (open), then house 0 (open), then back to house 1 (now closed). Total count is 3.
example_3.py — Single House
$
Input:
street = [Open], k = 10
Starting at house 0
›
Output:
1
💡 Note:
Edge case with only one house. We close the door, move right (which brings us back to the same house since it's circular), find it's closed, so return count of 1.
Constraints
- 1 ≤ number of houses ≤ k
- k ≤ 103
- At least one door is guaranteed to be open initially
- The street is circular (house n connects back to house 1)
- You can only move clockwise (right direction)
Visualization
Tap to expand
Understanding the Visualization
1
Create Landmark
Close the starting door to create a unique marker that will help us detect when we've completed a full circle
2
Begin Counting
Move clockwise and start counting houses. Each house represents one unit in our final count
3
Continue Journey
Keep moving clockwise through open doors, incrementing our house counter with each move
4
Detect Completion
When we encounter a closed door, we know we've returned to our starting point and completed exactly one full circle
Key Takeaway
🎯 Key Insight: By closing the starting door as a landmark, we create a perfect stopping condition that guarantees exactly one full traversal of the circular street, making the counting both accurate and efficient!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code