Skip to content

Commit 89e7a96

Browse files
committed
feat: add solutions to lc problem: No.2728
No.2728.Count Houses in a Circular Street
1 parent 2e210c8 commit 89e7a96

File tree

7 files changed

+382
-6
lines changed

7 files changed

+382
-6
lines changed

solution/2700-2799/2728.Count Houses in a Circular Street/README.md

+134-3
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,165 @@ The number of houses is equal to k, which is 5.
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:遍历**
56+
57+
我们先循环 $k$ 次,每次打开当前房子的门,然后向左移动一格,循环结束后,所有房子的门都是打开的。
58+
59+
然后,我们再循环左移,如果当前房子的门是打开的,就关闭它,房子数加一,继续左移,直到当前房子的门是关闭的,循环结束,返回房子数。
60+
61+
时间复杂度 $O(k)$,其中 $k$ 为题目给定的整数。空间复杂度 $O(1)$。
62+
5563
<!-- tabs:start -->
5664

5765
### **Python3**
5866

5967
<!-- 这里可写当前语言的特殊实现逻辑 -->
6068

6169
```python
62-
70+
# Definition for a street.
71+
# class Street:
72+
# def openDoor(self):
73+
# pass
74+
# def closeDoor(self):
75+
# pass
76+
# def isDoorOpen(self):
77+
# pass
78+
# def moveRight(self):
79+
# pass
80+
# def moveLeft(self):
81+
# pass
82+
class Solution:
83+
def houseCount(self, street: Optional["Street"], k: int) -> int:
84+
for _ in range(k):
85+
street.openDoor()
86+
street.moveLeft()
87+
ans = 0
88+
while street.isDoorOpen():
89+
street.closeDoor()
90+
street.moveLeft()
91+
ans += 1
92+
return ans
6393
```
6494

6595
### **Java**
6696

6797
<!-- 这里可写当前语言的特殊实现逻辑 -->
6898

6999
```java
70-
100+
/**
101+
* Definition for a street.
102+
* class Street {
103+
* public Street(int[] doors);
104+
* public void openDoor();
105+
* public void closeDoor();
106+
* public boolean isDoorOpen();
107+
* public void moveRight();
108+
* public void moveLeft();
109+
* }
110+
*/
111+
class Solution {
112+
public int houseCount(Street street, int k) {
113+
while (k-- > 0) {
114+
street.openDoor();
115+
street.moveLeft();
116+
}
117+
int ans = 0;
118+
while (street.isDoorOpen()) {
119+
++ans;
120+
street.closeDoor();
121+
street.moveLeft();
122+
}
123+
return ans;
124+
}
125+
}
71126
```
72127

73128
### **C++**
74129

75130
```cpp
76-
131+
/**
132+
* Definition for a street.
133+
* class Street {
134+
* public:
135+
* Street(vector<int> doors);
136+
* void openDoor();
137+
* void closeDoor();
138+
* bool isDoorOpen();
139+
* void moveRight();
140+
* void moveLeft();
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
int houseCount(Street* street, int k) {
146+
while (k--) {
147+
street->openDoor();
148+
street->moveLeft();
149+
}
150+
int ans = 0;
151+
while (street->isDoorOpen()) {
152+
ans++;
153+
street->closeDoor();
154+
street->moveLeft();
155+
}
156+
return ans;
157+
}
158+
};
77159
```
78160
79161
### **Go**
80162
81163
```go
164+
/**
165+
* Definition for a street.
166+
* type Street interface {
167+
* OpenDoor()
168+
* CloseDoor()
169+
* IsDoorOpen() bool
170+
* MoveRight()
171+
* MoveLeft()
172+
* }
173+
*/
174+
func houseCount(street Street, k int) (ans int) {
175+
for ; k > 0; k-- {
176+
street.OpenDoor()
177+
street.MoveLeft()
178+
}
179+
for ; street.IsDoorOpen(); street.MoveLeft() {
180+
ans++
181+
street.CloseDoor()
182+
}
183+
return
184+
}
185+
```
82186

187+
### **TypeScript**
188+
189+
```ts
190+
/**
191+
* Definition for a street.
192+
* class Street {
193+
* constructor(doors: number[]);
194+
* public openDoor(): void;
195+
* public closeDoor(): void;
196+
* public isDoorOpen(): boolean;
197+
* public moveRight(): void;
198+
* public moveLeft(): void;
199+
* }
200+
*/
201+
function houseCount(street: Street | null, k: number): number {
202+
while (k-- > 0) {
203+
street.openDoor();
204+
street.moveLeft();
205+
}
206+
let ans = 0;
207+
while (street.isDoorOpen()) {
208+
++ans;
209+
street.closeDoor();
210+
street.moveLeft();
211+
}
212+
return ans;
213+
}
83214
```
84215

85216
### **...**

solution/2700-2799/2728.Count Houses in a Circular Street/README_EN.md

+126-3
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,148 @@ The number of houses is equal to k, which is 5.
5353
### **Python3**
5454

5555
```python
56-
56+
# Definition for a street.
57+
# class Street:
58+
# def openDoor(self):
59+
# pass
60+
# def closeDoor(self):
61+
# pass
62+
# def isDoorOpen(self):
63+
# pass
64+
# def moveRight(self):
65+
# pass
66+
# def moveLeft(self):
67+
# pass
68+
class Solution:
69+
def houseCount(self, street: Optional["Street"], k: int) -> int:
70+
for _ in range(k):
71+
street.openDoor()
72+
street.moveLeft()
73+
ans = 0
74+
while street.isDoorOpen():
75+
street.closeDoor()
76+
street.moveLeft()
77+
ans += 1
78+
return ans
5779
```
5880

5981
### **Java**
6082

6183
```java
62-
84+
/**
85+
* Definition for a street.
86+
* class Street {
87+
* public Street(int[] doors);
88+
* public void openDoor();
89+
* public void closeDoor();
90+
* public boolean isDoorOpen();
91+
* public void moveRight();
92+
* public void moveLeft();
93+
* }
94+
*/
95+
class Solution {
96+
public int houseCount(Street street, int k) {
97+
while (k-- > 0) {
98+
street.openDoor();
99+
street.moveLeft();
100+
}
101+
int ans = 0;
102+
while (street.isDoorOpen()) {
103+
++ans;
104+
street.closeDoor();
105+
street.moveLeft();
106+
}
107+
return ans;
108+
}
109+
}
63110
```
64111

65112
### **C++**
66113

67114
```cpp
68-
115+
/**
116+
* Definition for a street.
117+
* class Street {
118+
* public:
119+
* Street(vector<int> doors);
120+
* void openDoor();
121+
* void closeDoor();
122+
* bool isDoorOpen();
123+
* void moveRight();
124+
* void moveLeft();
125+
* };
126+
*/
127+
class Solution {
128+
public:
129+
int houseCount(Street* street, int k) {
130+
while (k--) {
131+
street->openDoor();
132+
street->moveLeft();
133+
}
134+
int ans = 0;
135+
while (street->isDoorOpen()) {
136+
ans++;
137+
street->closeDoor();
138+
street->moveLeft();
139+
}
140+
return ans;
141+
}
142+
};
69143
```
70144
71145
### **Go**
72146
73147
```go
148+
/**
149+
* Definition for a street.
150+
* type Street interface {
151+
* OpenDoor()
152+
* CloseDoor()
153+
* IsDoorOpen() bool
154+
* MoveRight()
155+
* MoveLeft()
156+
* }
157+
*/
158+
func houseCount(street Street, k int) (ans int) {
159+
for ; k > 0; k-- {
160+
street.OpenDoor()
161+
street.MoveLeft()
162+
}
163+
for ; street.IsDoorOpen(); street.MoveLeft() {
164+
ans++
165+
street.CloseDoor()
166+
}
167+
return
168+
}
169+
```
74170

171+
### **TypeScript**
172+
173+
```ts
174+
/**
175+
* Definition for a street.
176+
* class Street {
177+
* constructor(doors: number[]);
178+
* public openDoor(): void;
179+
* public closeDoor(): void;
180+
* public isDoorOpen(): boolean;
181+
* public moveRight(): void;
182+
* public moveLeft(): void;
183+
* }
184+
*/
185+
function houseCount(street: Street | null, k: number): number {
186+
while (k-- > 0) {
187+
street.openDoor();
188+
street.moveLeft();
189+
}
190+
let ans = 0;
191+
while (street.isDoorOpen()) {
192+
++ans;
193+
street.closeDoor();
194+
street.moveLeft();
195+
}
196+
return ans;
197+
}
75198
```
76199

77200
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a street.
3+
* class Street {
4+
* public:
5+
* Street(vector<int> doors);
6+
* void openDoor();
7+
* void closeDoor();
8+
* bool isDoorOpen();
9+
* void moveRight();
10+
* void moveLeft();
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
int houseCount(Street* street, int k) {
16+
while (k--) {
17+
street->openDoor();
18+
street->moveLeft();
19+
}
20+
int ans = 0;
21+
while (street->isDoorOpen()) {
22+
ans++;
23+
street->closeDoor();
24+
street->moveLeft();
25+
}
26+
return ans;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a street.
3+
* type Street interface {
4+
* OpenDoor()
5+
* CloseDoor()
6+
* IsDoorOpen() bool
7+
* MoveRight()
8+
* MoveLeft()
9+
* }
10+
*/
11+
func houseCount(street Street, k int) (ans int) {
12+
for ; k > 0; k-- {
13+
street.OpenDoor()
14+
street.MoveLeft()
15+
}
16+
for ; street.IsDoorOpen(); street.MoveLeft() {
17+
ans++
18+
street.CloseDoor()
19+
}
20+
return
21+
}

0 commit comments

Comments
 (0)