返回介绍

solution / 2700-2799 / 2728.Count Houses in a Circular Street / README_EN

发布于 2024-06-17 01:03:00 字数 5138 浏览 0 评论 0 收藏 0

2728. Count Houses in a Circular Street

中文文档

Description

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.

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 openDoor(): Open the door of the house you are in front of.
  • 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.
  • void moveLeft(): Move to the left house.

Return ans _which represents the number of houses on this street._

 

Example 1:

Input: street = [0,0,0,0], k = 10
Output: 4
Explanation: There are 4 houses, and all their doors are closed. 
The number of houses is less than k, which is 10.

Example 2:

Input: street = [1,0,1,1,0], k = 5
Output: 5
Explanation: There are 5 houses, and the doors of the 1st, 3rd, and 4th house (moving in the right direction) are open, and the rest are closed.
The number of houses is equal to k, which is 5.

 

Constraints:

  • n == number of houses
  • 1 <= n <= k <= 103

Solutions

Solution 1

# 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
/**
 * Definition for a street.
 * class Street {
 *   public Street(int[] doors);
 *   public void openDoor();
 *   public void closeDoor();
 *   public boolean isDoorOpen();
 *   public void moveRight();
 *   public void moveLeft();
 * }
 */
class Solution {
  public int houseCount(Street street, int k) {
    while (k-- > 0) {
      street.openDoor();
      street.moveLeft();
    }
    int ans = 0;
    while (street.isDoorOpen()) {
      ++ans;
      street.closeDoor();
      street.moveLeft();
    }
    return ans;
  }
}
/**
 * Definition for a street.
 * class Street {
 * public:
 *   Street(vector<int> doors);
 *   void openDoor();
 *   void closeDoor();
 *   bool isDoorOpen();
 *   void moveRight();
 *   void moveLeft();
 * };
 */
class Solution {
public:
  int houseCount(Street* street, int k) {
    while (k--) {
      street->openDoor();
      street->moveLeft();
    }
    int ans = 0;
    while (street->isDoorOpen()) {
      ans++;
      street->closeDoor();
      street->moveLeft();
    }
    return ans;
  }
};
/**
 * Definition for a street.
 * type Street interface {
 *   OpenDoor()
 *   CloseDoor()
 *   IsDoorOpen() bool
 *   MoveRight()
 *   MoveLeft()
 * }
 */
func houseCount(street Street, k int) (ans int) {
  for ; k > 0; k-- {
    street.OpenDoor()
    street.MoveLeft()
  }
  for ; street.IsDoorOpen(); street.MoveLeft() {
    ans++
    street.CloseDoor()
  }
  return
}
/**
 * Definition for a street.
 * class Street {
 *   constructor(doors: number[]);
 *   public openDoor(): void;
 *   public closeDoor(): void;
 *   public isDoorOpen(): boolean;
 *   public moveRight(): void;
 *   public moveLeft(): void;
 * }
 */
function houseCount(street: Street | null, k: number): number {
  while (k-- > 0) {
    street.openDoor();
    street.moveLeft();
  }
  let ans = 0;
  while (street.isDoorOpen()) {
    ++ans;
    street.closeDoor();
    street.moveLeft();
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文