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 (at least one is open).
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 closeDoor(): Close the door of the house you are in front of.
boolean isDoorOpen(): Returns true if the door of the current house is open and false otherwise.
void moveRight(): Move to the right house.
Note that by circular street, we mean if you number the houses from
1 to n, then the right house of housei is housei+1 for i < n, and the right house of housen is house1.
Return answhich represents the number of houses on this street.
Input: street =[1,1,1,1], k =10Output: 4Explanation: There are 4 houses, and all their doors are open.The number of houses is less than k, which is10.
Example 2:
1
2
3
4
Input: street =[1,0,1,1,0], k =5Output: 5Explanation: 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 is5.
Constraints:
n == number of houses
1 <= n <= k <= 10^5
street is circular by definition provided in the statement.
The input is generated such that at least one of the doors is open.
Since the street is circular and at least one door is open, we can close each open door as we visit it, moving right each time. When we return to a house with a closed door, we’ve completed a full circle and counted all houses.
// The Street API is provided by the platform. We declare the interface here// for documentation purposes and implement the same logic as other languages.typeStreetinterface {
CloseDoor()
IsDoorOpen() boolMoveRight()
}
// houseCount returns the number of houses in the circular street.funchouseCount(streetStreet, kint) int {
cnt:=0ifstreet.IsDoorOpen() {
street.CloseDoor()
cnt++ }
fori:=1; i < k; i++ {
street.MoveRight()
ifstreet.IsDoorOpen() {
street.CloseDoor()
cnt++ } else {
break }
}
returncnt}
// The Street API is provided by the platform; signature shown for clarity.
interfaceStreet {
funcloseDoor()
funisDoorOpen(): Boolean
funmoveRight()
}
classSolution {
funhouseCount(street: Street, k: Int): Int {
var cnt = 0if (street.isDoorOpen()) {
street.closeDoor()
cnt++ }
for (i in1 until k) {
street.moveRight()
if (street.isDoorOpen()) {
street.closeDoor()
cnt++ } else {
break }
}
return cnt
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# This is the Street's API interface.# class Street:# def closeDoor(self):# def isDoorOpen(self) -> bool:# def moveRight(self):classSolution:
defhouseCount(self, street: 'Street', k: int) -> int:
cnt =0if street.isDoorOpen():
street.closeDoor()
cnt +=1for _ in range(1, k):
street.moveRight()
if street.isDoorOpen():
street.closeDoor()
cnt +=1else:
breakreturn cnt
// The Street interface is provided by the platform; we declare it here
// for documentation and type-checking within the note.
interfaceStreet {
closeDoor():void;
isDoorOpen():boolean;
moveRight():void;
}
classSolution {
publichouseCount(street: Street, k: number):number {
letcnt=0;
if (street.isDoorOpen()) {
street.closeDoor();
cnt++;
}
for (leti=1; i<k; i++) {
street.moveRight();
if (street.isDoorOpen()) {
street.closeDoor();
cnt++;
} else {
break;
}
}
returncnt;
}
}