当前位置:网站首页>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;
边栏推荐
- Day 248/300 thoughts on how graduates find jobs
- (practice C language every day) reverse linked list II
- Leetcode daily question (1997. first day where you have been in all the rooms)
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- Monotonic stack
- Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- 从autojs到冰狐智能辅助的心里历程
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
猜你喜欢
Huawei equipment configuration ospf-bgp linkage
Introduction and underlying analysis of regular expressions
BUU的MISC(不定时更新)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
My seven years with NLP
Visitor tweets about how you can layout the metauniverse
SAP SD发货流程中托盘的管理
Do you really know the use of idea?
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
随机推荐
C language_ Double create, pre insert, post insert, traverse, delete
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
我的创作纪念日
Fedora/rehl installation semanage
Fedora/REHL 安装 semanage
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
Pymongo gets a list of data
Leetcode daily question (1870. minimum speed to arrive on time)
Office doc add in - Online CS
攻防世界 MISC中reverseMe简述
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
成功解决TypeError: data type ‘category‘ not understood
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
19.段页结合的实际内存管理
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
SSO process analysis
Windows Server 2016 standard installing Oracle
前缀和数组系列