HardNo. 2753LeetCode Premium

Count Houses in a Circular Street II — LeetCode 2753 Python Solution

Topics
General
Reading time
3 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 (at least one is open).

Example

Input
street = [1,1,1,1], k = 10
Output
4
Explanation
There are 4 houses, and all their doors are open.

Python solution

PythonSolution
# 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

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0192Word FrequencyMedium
0193Valid Phone NumbersEasy
0194Transpose FileMedium
0195Tenth LineEasy
0228Summary RangesEasy

Frequently asked questions

How hard is LeetCode 2753. Count Houses in a Circular Street II?

LeetCode 2753. Count Houses in a Circular Street II is rated Hard on LeetCode.

What topics does LeetCode 2753. Count Houses in a Circular Street II cover?

LeetCode 2753. Count Houses in a Circular Street II is tagged General on LeetCode.

Is LeetCode 2753. Count Houses in a Circular Street II a premium problem?

Yes. LeetCode 2753. Count Houses in a Circular Street II 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.

Free to download. Check it runs on your machine, then pick a plan.