862. Count Houses in a Circular Street II
You are given an object street of class Street, representing a circular street, and a positive integer k, which serves as an upper bound for the number of houses on this street (i.e., the number of houses is at most k). Each house has a door that may initially be either open or closed, with the guarantee that at least one door is open.
You start positioned in front of one of these houses, and your goal is to determine the exact count of houses on the street.
The Street class provides the following methods to assist in your task :
- void closeDoor() : Closes the door of the house you are currently facing.
- boolean isDoorOpen() : Returns true if the door of the house you are currently facing is open, otherwise returns false.
- void moveRight() : Moves to the next house in the circular street (i.e., after the last house, it wraps around to the first house).
Since the street is circular, numbering the houses from 1 to n, moving right from house i leads to house i+1 for i < n, and from house n, it loops back to house 1.
Return ans, which represents the total number of houses in the street.
Example 1:
Input : street = [1,1,1,1], k = 10
Output : 4
Explanation: There are 4 houses, all with open doors. The total number of houses (4) is within the given upper bound (k = 10).
Example 2:
Input : street = [1,0,1,1,0], k = 5
Output : 5
Explanation: There are 5 houses. The doors of the 1st, 3rd, and 4th houses (moving right) are open, while the rest are closed. The count matches the upper bound (k = 5).
Now Your Turn!
Pick the correct output for the given inputInput : street = [1,0,0,0,1], k = 5
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n represents the number of houses.
- 1 ≤ n ≤ k ≤ 10⁵
- The street is circular as defined above.
- The input guarantees that at least one house has an open door.