当前位置:网站首页>LeetCode 6109. 知道秘密的人数
LeetCode 6109. 知道秘密的人数
2022-07-05 18:12:00 【HumbleFool】
LeetCode 6109. 知道秘密的人数
线性DP + 前缀和优化
/* f[i][j]表示前i天,记得秘密j天的数量 j == 1: 一共有 delay <= j < forget数量 f[i][j] = f[i - 1][delay] + f[i - 1][delay + 1] ... f[i - 1][forget - 1]; 这里求了连续和,暴力n^3,会超时,前缀和优化 f[i][j] = f[i - 1][forget - 1] - f[i - 1][delay - 1]; j > 1: f[i][j] = f[i - 1][j - 1]; f[i][j] = f[i - 1][j - 1] - f[i - 1][j - 2] */
const int N = 1010, MOD = 1e9 + 7;
class Solution {
public:
int f[N][N];
int peopleAwareOfSecret(int n, int delay, int forget) {
for(int i = 1; i <= forget; i ++) f[1][i] = 1;
for(int i = 2; i <= n; i ++)
{
for(int j = 1; j <= forget; j ++)
{
if(j > 1) f[i][j] = (f[i - 1][j - 1] - f[i - 1][j - 2]) % MOD;
else if(j == 1) f[i][j] = (f[i - 1][forget - 1] - f[i - 1][delay - 1]) % MOD;
f[i][j] = (f[i][j] + f[i][j - 1]) % MOD;
}
}
return (f[n][forget] + MOD) % MOD;
}
};
边栏推荐
- 消除`if()else{ }`写法
- Login and connect CDB and PDB
- Multithreading (I) processes and threads
- LeetCode笔记:Weekly Contest 300
- node_exporter内存使用率不显示
- ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
- Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
- vulnhub之darkhole_2
- [BeanShell] there are many ways to write data locally
- Eliminate the writing of 'if () else{}'
猜你喜欢

Cmake tutorial step1 (basic starting point)

如何获取飞机穿过雷达两端的坐标

Tupu software digital twin | visual management system based on BIM Technology

图像分类,看我就够啦!

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

ViewPager + RecyclerView的内存泄漏

ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V

修复漏洞 - mysql 、es

Copy the linked list with random pointer in the "Li Kou brush question plan"

About Estimation with Cross-Validation
随机推荐
Wu Enda team 2022 machine learning course, coming
登录连接 CDB 和 PDB
Electron安装问题
LeetCode 6111. 螺旋矩阵 IV
怎么选择外盘期货平台最正规安全?
[performance test] full link voltage test
Einstein sum einsum
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
Matlab built-in function how different colors, matlab subsection function different colors drawing
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
英语句式参考
[paddleclas] common commands
Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
ConvMAE(2022-05)
Writing writing writing
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
[use electron to develop desktop on youqilin]
Clickhouse (03) how to install and deploy Clickhouse