Skip to content

Commit 8d5a3a4

Browse files
authored
Create count-houses-in-a-circular-street.cpp
1 parent d2f5f9e commit 8d5a3a4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(k)
2+
// Space: O(1)
3+
4+
// constructive algorithms
5+
class Solution {
6+
public:
7+
int houseCount(Street* street, int k) {
8+
for (int _ = 0; _ < k; ++_) {
9+
if (street->isDoorOpen()) {
10+
street->closeDoor();
11+
}
12+
street->moveRight();
13+
}
14+
int result = 0;
15+
for (; result < k; ++result) {
16+
if (street->isDoorOpen()) {
17+
break;
18+
}
19+
street->openDoor();
20+
street->moveRight();
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)