当前位置:网站首页>leetcode6109. 知道秘密的人数(中等,周赛)
leetcode6109. 知道秘密的人数(中等,周赛)
2022-07-06 06:55:00 【重you小垃】



思路:dp + 前缀和
具体思路:
dp[i]表示第i天新增的人数,而不是第i天知道秘密的人数
dp[i]表示第i天知道秘密的人数,逻辑有问题,存在重复…
状态转移方程:
dp[i] = sum(dp[i-forget+1]…dp[i-delay])
ans:sum(dp[n-forget+1]…dp[n])
class Solution {
public:
const int mod = 1e9 + 7;
int peopleAwareOfSecret(int n, int delay, int forget) {
vector<long long> dp(n + 1), sum(n + 1);
dp[1] = 1;
sum[1] = 1;
for (int i = 2; i <= n; ++i) {
dp[i]=(sum[max(i - delay, 0)]) - sum[max(i - forget, 0)];
dp[i] %= mod;
sum[i] = (dp[i] + sum[i - 1]) % mod;;
}
return ((sum[n] - sum[max(0, n - forget)]) % mod + mod) % mod;;
}
};
代码技巧1:
i - delay >= 0 ? sum[i - delay] : 0 可以写成:sum[max(i - delay, 0)]
代码技巧2:
防止ans:(sum[n] - sum[max(0, n - forget)]) % mod 出现负数,再 ( + mod) % mod;
边栏推荐
- The difference between get and post request types
- LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
- Reflex WMS medium level series 3: display shipped replaceable groups
- Delete external table source data
- UDP攻击是什么意思?UDP攻击防范措施
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- Database basics exercise part 2
- 雲上有AI,讓地球科學研究更省力
- ROS learning_ Basics
- 18.多级页表与快表
猜你喜欢

接口自动化测试框架:Pytest+Allure+Excel

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent

ROS learning_ Basics

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

《从0到1:CTFer成长之路》书籍配套题目(周更)

Windows Server 2016 standard installing Oracle

18.多级页表与快表

How to find a medical software testing institution? First flight software evaluation is an expert

ROS学习_基础
随机推荐
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
A brief introduction of reverseme in misc in the world of attack and defense
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
LeetCode - 152 乘积最大子数组
Every API has its foundation when a building rises from the ground
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
My seven years with NLP
kubernetes集群搭建Zabbix监控平台
BUU的MISC(不定时更新)
Do you really know the use of idea?
hydra常用命令
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
C language_ Double create, pre insert, post insert, traverse, delete
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Delete external table source data
18. Multi level page table and fast table
Call, apply, bind rewrite, easy to understand with comments
ROS learning_ Basics
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
云上有AI,让地球科学研究更省力