2728. Count Houses in a Circular Street π
Description
You are given an object street of class Street that represents a circular street and a positive integer k which represents a maximum bound for the number of houses in that street (in other words, the number of houses is less than or equal to k). Houses' doors could be open or closed initially.
Initially, you are standing in front of a door to a house on this street. Your task is to count the number of houses in the street.
The class Street contains the following functions which may help you:
void openDoor(): Open the door of the house you are in front of.void closeDoor(): Close the door of the house you are in front of.boolean isDoorOpen(): Returnstrueif the door of the current house is open andfalseotherwise.void moveRight(): Move to the right house.void moveLeft(): Move to the left house.
Return ans which represents the number of houses on this street.
Example 1:
Input: street = [0,0,0,0], k = 10 Output: 4 Explanation: There are 4 houses, and all their doors are closed. The number of houses is less than k, which is 10.
Example 2:
Input: street = [1,0,1,1,0], k = 5 Output: 5 Explanation: There are 5 houses, and the doors of the 1st, 3rd, and 4th house (moving in the right direction) are open, and the rest are closed. The number of houses is equal to k, which is 5.
Constraints:
n == number of houses1 <= n <= k <= 103
Solutions
Solution 1
Thinking
There are at most \(k\) houses on a circle, and we can only open or close doors and step left or right. Closing everything first and counting a lap cannot tell the start door from a finished lap.
Walk left \(k\) times while opening doors so the whole ring is open. Keep stepping left: close each open door and count, and stop at the first already-closed door. The count is the number of houses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |