返回介绍

solution / 1900-1999 / 1997.First Day Where You Have Been in All the Rooms / README_EN

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

1997. First Day Where You Have Been in All the Rooms

中文文档

Description

There are n rooms you need to visit, labeled from 0 to n - 1. Each day is labeled, starting from 0. You will go in and visit one room a day.

Initially on day 0, you visit room 0. The order you visit the rooms for the coming days is determined by the following rules and a given 0-indexed array nextVisit of length n:

  • Assuming that on a day, you visit room i,
  • if you have been in room i an odd number of times (including the current visit), on the next day you will visit a room with a lower or equal room number specified by nextVisit[i] where 0 <= nextVisit[i] <= i;
  • if you have been in room i an even number of times (including the current visit), on the next day you will visit room (i + 1) mod n.

Return _the label of the first day where you have been in all the rooms_. It can be shown that such a day exists. Since the answer may be very large, return it modulo 109 + 7.

 

Example 1:

Input: nextVisit = [0,0]
Output: 2
Explanation:
- On day 0, you visit room 0. The total times you have been in room 0 is 1, which is odd.
  On the next day you will visit room nextVisit[0] = 0
- On day 1, you visit room 0, The total times you have been in room 0 is 2, which is even.
  On the next day you will visit room (0 + 1) mod 2 = 1
- On day 2, you visit room 1. This is the first day where you have been in all the rooms.

Example 2:

Input: nextVisit = [0,0,2]
Output: 6
Explanation:
Your room visiting order for each day is: [0,0,1,0,0,1,2,...].
Day 6 is the first day where you have been in all the rooms.

Example 3:

Input: nextVisit = [0,1,2,0]
Output: 6
Explanation:
Your room visiting order for each day is: [0,0,1,1,2,2,3,...].
Day 6 is the first day where you have been in all the rooms.

 

Constraints:

  • n == nextVisit.length
  • 2 <= n <= 105
  • 0 <= nextVisit[i] <= i

Solutions

Solution 1

class Solution:
  def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
    n = len(nextVisit)
    f = [0] * n
    mod = 10**9 + 7
    for i in range(1, n):
      f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1) % mod
    return f[-1]
class Solution {
  public int firstDayBeenInAllRooms(int[] nextVisit) {
    int n = nextVisit.length;
    long[] f = new long[n];
    final int mod = (int) 1e9 + 7;
    for (int i = 1; i < n; ++i) {
      f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
    }
    return (int) f[n - 1];
  }
}
class Solution {
public:
  int firstDayBeenInAllRooms(vector<int>& nextVisit) {
    int n = nextVisit.size();
    vector<long long> f(n);
    const int mod = 1e9 + 7;
    for (int i = 1; i < n; ++i) {
      f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
    }
    return f[n - 1];
  }
};
func firstDayBeenInAllRooms(nextVisit []int) int {
  n := len(nextVisit)
  f := make([]int, n)
  const mod = 1e9 + 7
  for i := 1; i < n; i++ {
    f[i] = (f[i-1] + 1 + f[i-1] - f[nextVisit[i-1]] + 1 + mod) % mod
  }
  return f[n-1]
}

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

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

发布评论

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