当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

Cmake tutorial step1 (basic starting point)

彻底理解为什么网络 I/O 会被阻塞?

Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.

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

IDC report: Tencent cloud database ranks top 2 in the relational database market!

Thoroughly understand why network i/o is blocked?
![[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](/img/a6/aa0b8d30913dc64f3c0cd891528c40.png)
[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

Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry

Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具

Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
随机推荐
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
项目中遇到的问题 u-parse 组件渲染问题
Simulate the hundred prisoner problem
[paddleclas] common commands
Sophon autocv: help AI industrial production and realize visual intelligent perception
开户复杂吗?网上开户安全么?
OpenShift常用管理命令杂记
U-Net: Convolutional Networks for Biomedical Images Segmentation
从XML架构生成类
【在優麒麟上使用Electron開發桌面應】
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
ClickHouse(03)ClickHouse怎么安装和部署
开户注册挖财安全吗?有没有风险的?靠谱吗?
Check namespaces and classes
Leetcode exercise - 206 Reverse linked list
Einstein sum einsum
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图