当前位置:网站首页>Leetcode-6109: number of people who know secrets
Leetcode-6109: number of people who know secrets
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-6109: Number of people who know the secret
subject
Topic linking
In the 1
God , A man discovered a secret .
Give you an integer delay
, It means that everyone will find the secret delay
After heaven , Every day To a new person Share Secret . I'll give you an integer at the same time forget
, It means that everyone is discovering secrets forget
Days later forget The secret . A person You can't Share secrets on and after the day you forget them .
Give you an integer n
, Please go back to n
At the end of the day , Number of people who know the secret . Because the answer could be big , Please put the result to 10^9 + 7
Remainder After the return .
Example 1:
Input :n = 6, delay = 2, forget = 4
Output :5
explain :
The first 1 God : Suppose the first person is A .( One knows the secret )
The first 2 God :A Is the only one who knows the secret .( One knows the secret )
The first 3 God :A Share the secret with B .( Two people know the secret )
The first 4 God :A Share the secret with a new person C .( Three people know the secret )
The first 5 God :A Forget the secret ,B Share the secret with a new person D .( Three people know the secret )
The first 6 God :B Share the secret with E,C Share the secret with F .( Five people know the secret )
Example 2:
Input :n = 4, delay = 1, forget = 3
Output :6
explain :
The first 1 God : The first person who knows the secret A .( One knows the secret )
The first 2 God :A Share the secret with B .( Two people know the secret )
The first 3 God :A and B Share the secret with 2 A new person C and D .( Four people know the secret )
The first 4 God :A Forget the secret ,B、C、D Share with 3 A new person .( Six people know the secret )
Problem solving
Method 1 : simulation ( Overtime )
Set up two queues to simulate
- Delay queue : Already know the secret , But I can't share yet
- Share queue : Already know the password , And can share
All stored in the queue are expiration dates , When the element in the delay queue expires , Will pop up and join the sharing queue , The element in the sharing queue expires , It will pop up , Description forgot .
The last person who knows the secret , Is the total number of delay queues and sharing queues
shortcoming : But for this problem , Don't care who knows the secret , The topic scene is simple , This method is not optimal , Dynamic planning will be more appropriate .
class Solution {
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
queue<int> q_delay;
queue<int> q_shareing;
q_delay.push(1+delay);
for(int i=1;i<=n;i++){
while(!q_delay.empty()&&i>=q_delay.front()){
q_shareing.push(q_delay.front()+forget-delay);
q_delay.pop();
}
while(!q_shareing.empty()&&i>=q_shareing.front()){
q_shareing.pop();
}
for(int j=0;j<q_shareing.size();j++){
q_delay.push(i+delay);
}
}
return q_delay.size()+q_shareing.size();
}
};
Method 2 : Dynamic programming - Brush table method
According to the meaning , There are two kinds of people :
- A class : Know the secret , But I can't share yet ;
- B class : Know the secret , And can share .
dp[i] It means the first one i+1 God , Yes dp[i] individual B Humanoid
Pay special attention to the details of the index , For example, the equal sign .
about if(i+delay>=n), such as n=3,delay=2
The first day i=0, and i+delay=2, At this point, for the n For heaven's sake , It can't be counted A Human like because the next day became B Humanoid , So add an equal sign
in addition dp[0]=1, It does not accord with dp The definition of , Just to initialize , Convenient for subsequent calculation .
class Solution {
public:
const int MOD=1e9+7;
int peopleAwareOfSecret(int n, int delay, int forget) {
int cnt_a=0;
vector<int> dp(n);// Record No. i Days of B Humanoid
dp[0]=1;
for(int i=0;i<n;i++){
if(i+delay>=n) cnt_a=(cnt_a+dp[i])%MOD;// The first n Days of A Humanoid
for(int j=i+delay;j<min(n,i+forget);j++){
dp[j]=(dp[j]+dp[i])%MOD;
}
}
return (cnt_a+dp[n-1])%MOD;// People who know secrets =A Humanoid +B Humanoid
}
};
边栏推荐
- 1041 Be Unique
- Brief introduction to tcp/ip protocol stack
- 1039 Course List for Student
- [practical skills] technical management of managers with non-technical background
- Daily question 1984 Minimum difference in student scores
- 【Rust 笔记】17-并发(上)
- 【实战技能】如何做好技术培训?
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- Introduction et expérience de wazuh open source host Security Solution
- Introduction and experience of wazuh open source host security solution
猜你喜欢
How to adjust bugs in general projects ----- take you through the whole process by hand
MIT-6874-Deep Learning in the Life Sciences Week 7
CF1637E Best Pair
开源存储这么香,为何我们还要坚持自研?
F - Two Exam(AtCoder Beginner Contest 238)
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Dynamic planning solution ideas and summary (30000 words)
QQ电脑版取消转义符输入表情
MIT-6874-Deep Learning in the Life Sciences Week 7
随机推荐
One question per day 1447 Simplest fraction
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
Open source storage is so popular, why do we insist on self-development?
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Convolution neural network -- convolution layer
Doing SQL performance optimization is really eye-catching
2017 USP Try-outs C. Coprimes
[practical skills] technical management of managers with non-technical background
【Rust 笔记】15-字符串与文本(上)
EOJ 2021.10 E. XOR tree
redis发布订阅命令行实现
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
开源存储这么香,为何我们还要坚持自研?
CF1634E Fair Share
One question per day 1765 The highest point in the map
How to adjust bugs in general projects ----- take you through the whole process by hand
Dichotomy, discretization, etc
CF1634 F. Fibonacci Additions
个人开发的渗透测试工具Satania v1.2更新
Introduction et expérience de wazuh open source host Security Solution