当前位置:网站首页>Leetcode daily question (1997. first day where you have been in all the rooms)
Leetcode daily question (1997. first day where you have been in all the rooms)
2022-07-06 06:35:00 【wangjun861205】
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] = 0On 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 = 1On 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
This problem seems to have been done before , It may just be similar , But I have no impression at all
The questions I have done recently are cheap , The difficulty is not in solving problems , All in various details
hypothesis dp[i][odd] For an odd number of times into the i Steps required for a room , dp[i][even] Enter the... For an even number of times i Steps required for a room , Then we can push down the following two ;
dp[i][odd] = dp[i-1][even] + 1
dp[i][even] = dp[i][odd] + (dp[i][odd]-dp[k][odd]) + 1
among k = next_visit[i]
The first of these two is easy to understand , Only after Ou enters a room several times can he push back into a room . The second actual expression is , Enter the third even number of times i The premise of a room must be an odd number of times , Because it is an odd number of entries, we need to return to a certain point in front , Be careful , The title says to return to next_visit[i] Any point before , But what we want is the minimum distance , According to the rules of the topic , The farther the point we return from the current point , The number of steps we need to go back to the current point must be more , So don't worry , The return point we choose must be next_visit[i], So we walked again from next_visit[i] To i Distance of , So is dp[i][odd] - dp[k][odd] + 1. The whole calculation process needs attention , Because the results are done mod Operational , So there may be dp[i][odd] < dp[k][odd] The situation of , At this time, the result is negative , To avoid this situation, you can dp[i][odd] + 10.pow(9) + 7
use std::collections::HashMap;
impl Solution {
fn dp(
next_visit: &Vec<i32>,
i: usize,
is_odd: bool,
cache: &mut HashMap<(usize, bool), i128>,
) -> i128 {
let m = 10i128.pow(9) + 7;
if is_odd {
let next = if let Some(c) = cache.get(&(i - 1, false)) {
*c
} else {
Solution::dp(next_visit, i - 1, false, cache)
};
let ans = (next + 1) % m;
cache.insert((i, is_odd), ans);
return ans;
}
let odd = if let Some(c) = cache.get(&(i, true)) {
*c
} else {
Solution::dp(next_visit, i, true, cache)
};
let prev = if let Some(c) = cache.get(&(next_visit[i] as usize, true)) {
*c
} else {
Solution::dp(next_visit, next_visit[i] as usize, true, cache)
};
let ans = if 2 * odd < prev {
(2 * odd + m - prev + 1) % m
} else {
(2 * odd - prev + 1) % m
};
cache.insert((i, is_odd), ans);
ans
}
pub fn first_day_been_in_all_rooms(next_visit: Vec<i32>) -> i32 {
let mut cache = HashMap::new();
cache.insert((0, true), 0);
cache.insert((0, false), 1);
Solution::dp(&next_visit, next_visit.len() - 1, true, &mut cache) as i32
}
}
边栏推荐
- oscp raven2靶机渗透过程
- 钓鱼&文件名反转&office远程模板
- SQL Server manager studio(SSMS)安装教程
- Use shortcut LNK online CS
- Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
- Suspended else
- Selenium source code read through · 9 | desiredcapabilities class analysis
- Defense (greed), FBI tree (binary tree)
- Data type of MySQL
- 模拟卷Leetcode【普通】1219. 黄金矿工
猜你喜欢
Lesson 7 tensorflow realizes convolutional neural network
How effective is the Chinese-English translation of international economic and trade contracts
Use shortcut LNK online CS
Changes in the number of words in English papers translated into Chinese
My daily learning records / learning methods
[web security] nodejs prototype chain pollution analysis
专业论文翻译,英文摘要如何写比较好
Remember the implementation of a relatively complex addition, deletion and modification function based on jeecg-boot
[mqtt from getting started to improving series | 01] quickly build an mqtt test environment from 0 to 1
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
随机推荐
LeetCode 1200. Minimum absolute difference
Today's summer solstice
记一个基于JEECG-BOOT的比较复杂的增删改功能的实现
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
国际经贸合同翻译 中译英怎样效果好
Grouping convolution and DW convolution, residuals and inverted residuals, bottleneck and linearbottleneck
Simulation volume leetcode [general] 1405 Longest happy string
Making interactive page of "left tree and right table" based on jeecg-boot
Black cat takes you to learn UFS protocol Chapter 18: how UFS configures logical units (Lu Management)
The whole process realizes the single sign on function and the solution of "canceltoken" of undefined when the request is canceled
Py06 dictionary mapping dictionary nested key does not exist test key sorting
论文翻译英译中,怎样做翻译效果好?
How effective is the Chinese-English translation of international economic and trade contracts
翻译影视剧字幕,这些特点务必要了解
leetcode 24. 两两交换链表中的节点
CS certificate fingerprint modification
CS-证书指纹修改
模拟卷Leetcode【普通】1062. 最长重复子串
How to translate professional papers and write English abstracts better
Wish Dragon Boat Festival is happy