当前位置:网站首页>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;
边栏推荐
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- 自动化测试环境配置
- LeetCode Algorithm 2181. 合并零之间的节点
- Arduino tutorial - Simon games
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- 机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
- At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
- 【Hot100】739. Daily temperature
猜你喜欢

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

雲上有AI,讓地球科學研究更省力

Biomedical English contract translation, characteristics of Vocabulary Translation

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品

(practice C language every day) reverse linked list II

SQL Server manager studio(SSMS)安装教程

L'Ia dans les nuages rend la recherche géoscientifique plus facile

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

19. Actual memory management of segment page combination

Introduction and underlying analysis of regular expressions
随机推荐
PCL实现选框裁剪点云
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
LeetCode Algorithm 2181. 合并零之间的节点
【软件测试进阶第1步】自动化测试基础知识
Reflex WMS中阶系列3:显示已发货可换组
How to reconstruct the class explosion caused by m*n strategies?
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
What is the difference between int (1) and int (10)? Senior developers can't tell!
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Monotonic stack
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Fast target recognition based on pytorch and fast RCNN
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
19. Actual memory management of segment page combination
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)