Count Houses in a Circular Street II - 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). Initially, you are standing in front of a door to a house on this street.

Your task is to count the number of houses in the street.

The class Street contains the following functions which may help you:

  • void closeDoor(): Close the door of the house you are in front of.
  • boolean isDoorOpen(): Returns true if the door of the current house is open and false otherwise.
  • void moveRight(): Move to the right house.

Note that by circular street, we mean if you number the houses from 1 to n, then the right house of housei is housei+1 for i < n, and the right house of housen is house1.

Return ans which represents the number of houses on this street.

Input & Output

Example 1 — Mixed Open/Closed Doors
$ Input: houses = [true,false,true,true], k = 10
Output: 4
💡 Note: There are 4 houses in the circular street. Close the starting door, then walk right counting houses until we return to the closed door.
Example 2 — All Doors Open
$ Input: houses = [true,true,true], k = 4
Output: 3
💡 Note: 3 houses total. Close starting door, move right 3 times counting, then encounter the closed door we marked.
Example 3 — Large Street
$ Input: houses = [true,false,true,false,true,true,false], k = 20
Output: 7
💡 Note: 7 houses in the circular street. Our algorithm finds the exact count regardless of k value.

Constraints

  • n == number of houses
  • 1 ≤ n ≤ k ≤ 103
  • At least one door is initially open

Visualization

Tap to expand
Count Houses in a Circular Street II INPUT Circular Street Open Closed Open Open Input Values: houses = [true, false, true, true] k = 10 (max bound) At least one door is open ALGORITHM STEPS 1 Find Open Door Move until isDoorOpen() returns true 2 Mark Starting Point Close this door as marker closeDoor() 3 Count and Move moveRight(), count++ Close each open door 4 Return to Start Stop when all doors are closed (full circle) Traversal Progress: 1 2 3 4 All closed = done! FINAL RESULT 4 Houses OK Output 4 Total houses counted in circular street Key Insight: Mark and Return Strategy By closing the first open door we find, we create a unique marker. As we traverse the circular street, we close all doors and count houses. When we complete the circle and find all doors closed, we know we've counted every house exactly once. Time: O(n), Space: O(1). TutorialsPoint - Count Houses in a Circular Street II | Mark and Return Strategy
Asked in
Google 12 Facebook 8 Microsoft 6
12.5K Views
Medium Frequency
~15 min Avg. Time
246 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen