Count Houses in a Circular Street — LeetCode 2728 Python Solution
EasyLeetCode PremiumArrayInteractive
- Problem
- #2728
- Reading time
- 4 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- street = [0,0,0,0], k = 10
- Output
- 4
- Explanation
- There are 4 houses, and all their doors are closed.
Python solution
Python
# Definition for a street.
# class Street:
# def openDoor(self):
# pass
# def closeDoor(self):
# pass
# def isDoorOpen(self):
# pass
# def moveRight(self):
# pass
# def moveLeft(self):
# pass
class Solution:
def houseCount(self, street: Optional["Street"], k: int) -> int:
for _ in range(k):
street.openDoor()
street.moveLeft()
ans = 0
while street.isDoorOpen():
street.closeDoor()
street.moveLeft()
ans += 1
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2728. Count Houses in a Circular Street?
- LeetCode 2728. Count Houses in a Circular Street is rated Easy on LeetCode.
- What topics does LeetCode 2728. Count Houses in a Circular Street cover?
- LeetCode 2728. Count Houses in a Circular Street is tagged Array and Interactive on LeetCode.
- Is LeetCode 2728. Count Houses in a Circular Street a premium problem?
- Yes. LeetCode 2728. Count Houses in a Circular Street is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.