Skip to content

feat: add solutions to lc problem: No.2753 #1154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@

时间复杂度 $O(k)$,其中 $k$ 为题目给定的整数。空间复杂度 $O(1)$。

相似题目:

- [2753. 计算一个环形街道上的房屋数量 II](/solution/2700-2799/2753.Count%20Houses%20in%20a%20Circular%20Street%20II/README.md)

<!-- tabs:start -->

### **Python3**
Expand Down
133 changes: 130 additions & 3 deletions solution/2700-2799/2753.Count Houses in a Circular Street II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,161 @@

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

**方法一:脑筋急转弯**

我们注意到,题目中至少有一扇门是开着的,我们可以先找到其中一扇开着的门。

然后,我们跳过这扇开着的门,往右移动,每次移动时,计数器加一,如果遇到开着的门,就把门关上。那么答案就是最后一次遇到的开着的门时的计数器的值。

时间复杂度 $O(k)$,空间复杂度 $O(1)$。

相似题目:

- [2728. 计算一个环形街道上的房屋数量](/solution/2700-2799/2728.Count%20Houses%20in%20a%20Circular%20Street/README.md)

<!-- tabs:start -->

### **Python3**

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

```python

# Definition for a street.
# class Street:
# def closeDoor(self):
# pass
# def isDoorOpen(self):
# pass
# def moveRight(self):
# pass
class Solution:
def houseCount(self, street: Optional["Street"], k: int) -> int:
while not street.isDoorOpen():
street.moveRight()
for i in range(1, k + 1):
street.moveRight()
if street.isDoorOpen():
ans = i
street.closeDoor()
return ans
```

### **Java**

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

```java

/**
* Definition for a street.
* class Street {
* public Street(int[] doors);
* public void closeDoor();
* public boolean isDoorOpen();
* public void moveRight();
* }
*/
class Solution {
public int houseCount(Street street, int k) {
while (!street.isDoorOpen()) {
street.moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street.moveRight();
if (street.isDoorOpen()) {
ans = i;
street.closeDoor();
}
}
return ans;
}
}
```

### **C++**

```cpp

/**
* Definition for a street.
* class Street {
* public:
* Street(vector<int> doors);
* void closeDoor();
* bool isDoorOpen();
* void moveRight();
* };
*/
class Solution {
public:
int houseCount(Street* street, int k) {
while (!street->isDoorOpen()) {
street->moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street->moveRight();
if (street->isDoorOpen()) {
ans = i;
street->closeDoor();
}
}
return ans;
}
};
```

### **Go**

```go
/**
* Definition for a street.
* type Street interface {
* CloseDoor()
* IsDoorOpen() bool
* MoveRight()
* }
*/
func houseCount(street Street, k int) (ans int) {
for !street.IsDoorOpen() {
street.MoveRight()
}
for i := 1; i <= k; i++ {
street.MoveRight()
if street.IsDoorOpen() {
ans = i
street.CloseDoor()
}
}
return
}
```

### **TypeScript**

```ts
/**
* Definition for a street.
* class Street {
* constructor(doors: number[]);
* public closeDoor(): void;
* public isDoorOpen(): boolean;
* public moveRight(): void;
* }
*/
function houseCount(street: Street | null, k: number): number {
while (!street.isDoorOpen()) {
street.moveRight();
}
let ans = 0;
for (let i = 1; i <= k; ++i) {
street.moveRight();
if (street.isDoorOpen()) {
ans = i;
street.closeDoor();
}
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,140 @@ The number of houses is equal to k, which is 5.
### **Python3**

```python

# Definition for a street.
# class Street:
# def closeDoor(self):
# pass
# def isDoorOpen(self):
# pass
# def moveRight(self):
# pass
class Solution:
def houseCount(self, street: Optional["Street"], k: int) -> int:
while not street.isDoorOpen():
street.moveRight()
for i in range(1, k + 1):
street.moveRight()
if street.isDoorOpen():
ans = i
street.closeDoor()
return ans
```

### **Java**

```java

/**
* Definition for a street.
* class Street {
* public Street(int[] doors);
* public void closeDoor();
* public boolean isDoorOpen();
* public void moveRight();
* }
*/
class Solution {
public int houseCount(Street street, int k) {
while (!street.isDoorOpen()) {
street.moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street.moveRight();
if (street.isDoorOpen()) {
ans = i;
street.closeDoor();
}
}
return ans;
}
}
```

### **C++**

```cpp

/**
* Definition for a street.
* class Street {
* public:
* Street(vector<int> doors);
* void closeDoor();
* bool isDoorOpen();
* void moveRight();
* };
*/
class Solution {
public:
int houseCount(Street* street, int k) {
while (!street->isDoorOpen()) {
street->moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street->moveRight();
if (street->isDoorOpen()) {
ans = i;
street->closeDoor();
}
}
return ans;
}
};
```

### **Go**

```go
/**
* Definition for a street.
* type Street interface {
* CloseDoor()
* IsDoorOpen() bool
* MoveRight()
* }
*/
func houseCount(street Street, k int) (ans int) {
for !street.IsDoorOpen() {
street.MoveRight()
}
for i := 1; i <= k; i++ {
street.MoveRight()
if street.IsDoorOpen() {
ans = i
street.CloseDoor()
}
}
return
}
```

### **TypeScript**

```ts
/**
* Definition for a street.
* class Street {
* constructor(doors: number[]);
* public closeDoor(): void;
* public isDoorOpen(): boolean;
* public moveRight(): void;
* }
*/
function houseCount(street: Street | null, k: number): number {
while (!street.isDoorOpen()) {
street.moveRight();
}
let ans = 0;
for (let i = 1; i <= k; ++i) {
street.moveRight();
if (street.isDoorOpen()) {
ans = i;
street.closeDoor();
}
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a street.
* class Street {
* public:
* Street(vector<int> doors);
* void closeDoor();
* bool isDoorOpen();
* void moveRight();
* };
*/
class Solution {
public:
int houseCount(Street* street, int k) {
while (!street->isDoorOpen()) {
street->moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street->moveRight();
if (street->isDoorOpen()) {
ans = i;
street->closeDoor();
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a street.
* type Street interface {
* CloseDoor()
* IsDoorOpen() bool
* MoveRight()
* }
*/
func houseCount(street Street, k int) (ans int) {
for !street.IsDoorOpen() {
street.MoveRight()
}
for i := 1; i <= k; i++ {
street.MoveRight()
if street.IsDoorOpen() {
ans = i
street.CloseDoor()
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a street.
* class Street {
* public Street(int[] doors);
* public void closeDoor();
* public boolean isDoorOpen();
* public void moveRight();
* }
*/
class Solution {
public int houseCount(Street street, int k) {
while (!street.isDoorOpen()) {
street.moveRight();
}
int ans = 0;
for (int i = 1; i <= k; ++i) {
street.moveRight();
if (street.isDoorOpen()) {
ans = i;
street.closeDoor();
}
}
return ans;
}
}
Loading