当前位置:网站首页>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;
边栏推荐
- LeetCode每日一题(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)
- Do you really know the use of idea?
- pymongo获取一列数据
- A brief introduction of reverseme in misc in the world of attack and defense
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- LeetCode Algorithm 2181. 合并零之间的节点
- SQL Server manager studio(SSMS)安装教程
- Entity Developer数据库应用程序的开发
- Data security -- 13 -- data security lifecycle management
猜你喜欢
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
漏了监控:Zabbix对Eureka instance状态监控
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Leetcode daily question (971. flip binary tree to match preorder traversal)
My creation anniversary
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
What are the commonly used English words and sentences about COVID-19?
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
随机推荐
基于PyTorch和Fast RCNN快速实现目标识别
机器学习植物叶片识别
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
Successfully solved typeerror: data type 'category' not understood
SQL Server manager studio(SSMS)安装教程
Fedora/rehl installation semanage
L'Ia dans les nuages rend la recherche géoscientifique plus facile
The difference between get and post request types
Leetcode - 152 product maximum subarray
AI on the cloud makes earth science research easier
Delete external table source data
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
我的创作纪念日
Huawei equipment configuration ospf-bgp linkage
自动化测试环境配置
Monotonic stack
Basic commands of MySQL
Misc of BUU (update from time to time)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm