当前位置:网站首页>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
}
}
边栏推荐
- 红蓝对抗之流量加密(Openssl加密传输、MSF流量加密、CS修改profile进行流量加密)
- [Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
- Advanced MySQL: Basics (1-4 Lectures)
- 翻译公司证件盖章的价格是多少
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
- What are the commonly used English words and sentences about COVID-19?
- How to translate biomedical instructions in English
- Fledgling Xiao Li's 103rd blog CC2530 resource introduction
- Remember the implementation of a relatively complex addition, deletion and modification function based on jeecg-boot
- Mise en œuvre d’une fonction complexe d’ajout, de suppression et de modification basée sur jeecg - boot
猜你喜欢
论文翻译英译中,怎样做翻译效果好?
(practice C language every day) reverse linked list II
Advanced MySQL: Basics (1-4 Lectures)
专业论文翻译,英文摘要如何写比较好
Lecture 8: 1602 LCD (Guo Tianxiang)
Address bar parameter transmission of list page based on jeecg-boot
论文摘要翻译,多语言纯人工翻译
org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘outgoing‘ 开头的无效内容
Redis core technology and basic architecture of actual combat: what does a key value database contain?
我的创作纪念日
随机推荐
My daily learning records / learning methods
Play video with Tencent video plug-in in uni app
An article was uncovered to test the truth of outsourcing companies
Technology sharing | common interface protocol analysis
Summary of the post of "Web Test Engineer"
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
英语论文翻译成中文字数变化
Delete the variables added to watch1 in keil MDK
SQL Server manager studio(SSMS)安装教程
Chinese English comparison: you can do this Best of luck
论文翻译英译中,怎样做翻译效果好?
JDBC requset corresponding content and function introduction
Convert the array selected by El tree into an array object
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
Summary of leetcode's dynamic programming 4
Esp32 esp-idf watchdog twdt
MySQL is sorted alphabetically
LeetCode 1200. Minimum absolute difference