-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2728-count-houses-in-a-circular-street.js
More file actions
68 lines (64 loc) · 1.73 KB
/
2728-count-houses-in-a-circular-street.js
File metadata and controls
68 lines (64 loc) · 1.73 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* 2728. Count Houses in a Circular Street
* https://leetcode.com/problems/count-houses-in-a-circular-street/
* Difficulty: Easy
*
* 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(): Returns true if the door of the current house is open and false
* otherwise.
* - 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.
*/
/**
* Definition for a street.
* class Street {
* @param {number[]} doors
* constructor(doors);
*
* @return {void}
* openDoor();
*
* @return {void}
* closeDoor();
*
* @return {boolean}
* isDoorOpen();
*
* @return {void}
* moveRight();
*
* @return {void}
* moveLeft();
* }
*/
/**
* @param {Street} street
* @param {number} k
* @return {number}
*/
var houseCount = function(street, k) {
for (let i = 0; i < k; i++) {
street.closeDoor();
street.moveLeft();
}
street.openDoor();
let count = 1;
street.moveLeft();
while (!street.isDoorOpen()) {
street.moveLeft();
count++;
}
return count;
};