Count Houses in a Circular Street — LeetCode 2728 Python Solution

EasyLeetCode PremiumArrayInteractive
Problem
#2728
Reading time
4 min

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 ans

Complexity

MeasureComplexity
TimeO(n)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview