当前位置:网站首页>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;
边栏推荐
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- How to find a medical software testing institution? First flight software evaluation is an expert
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
- Reflex WMS中阶系列3:显示已发货可换组
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- hydra常用命令
- 漏了监控:Zabbix对Eureka instance状态监控
- Reflex WMS medium level series 3: display shipped replaceable groups
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
猜你喜欢

Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet

Introduction to ros2 installation and basic knowledge

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

RichView TRVStyle 模板样式的设置与使用

A brief introduction of reverseme in misc in the world of attack and defense

攻防世界 MISC中reverseMe简述

19.段页结合的实际内存管理

Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency

基于PyTorch和Fast RCNN快速实现目标识别
![[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
随机推荐
UWA Pipeline 2.2.1 版本更新说明
Machine learning plant leaf recognition
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
【Hot100】739. 每日温度
Fast target recognition based on pytorch and fast RCNN
My creation anniversary
C语言_双创建、前插,尾插,遍历,删除
中青看点阅读新闻
What is the difference between int (1) and int (10)? Senior developers can't tell!
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
MySQL high frequency interview 20 questions, necessary (important)
Automated test environment configuration
kubernetes集群搭建Zabbix监控平台
医疗软件检测机构怎么找,一航软件测评是专家
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
18. Multi level page table and fast table
[ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
ROS2安装及基础知识介绍
[unity] how to export FBX in untiy
云上有AI,让地球科学研究更省力