当前位置:网站首页>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;
边栏推荐
- 云上有AI,让地球科学研究更省力
- PCL实现选框裁剪点云
- 自动化测试环境配置
- Reflex WMS medium level series 3: display shipped replaceable groups
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- ROS learning_ Basics
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- 将ue4程序嵌入qt界面显示
- Database basics exercise part 2
- Basic commands of MySQL
猜你喜欢
ROS学习_基础
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Windows Server 2016 standard installing Oracle
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
[advanced software testing step 1] basic knowledge of automated testing
Simple use of MySQL database: add, delete, modify and query
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
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
随机推荐
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
【软件测试进阶第1步】自动化测试基础知识
How to find a medical software testing institution? First flight software evaluation is an expert
Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
【Hot100】739. 每日溫度
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Apache dolphin scheduler source code analysis (super detailed)
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
万丈高楼平地起,每个API皆根基
My seven years with NLP
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Call, apply, bind rewrite, easy to understand with comments
What are the commonly used English words and sentences about COVID-19?
医疗软件检测机构怎么找,一航软件测评是专家
自动化测试环境配置
ROS学习_基础
LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
What is the difference between int (1) and int (10)? Senior developers can't tell!