Leetcode 2327. Number of People Aware of a Secret [Python]
BFS版本会TLE,最后还是要DP。两个版本都写写吧。
BFS的话,while循环的控制是天数,而不是len(que).
class Solution:def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:Mod = 10**9 + 7container = collections.deque()container.append((delay, forget))Day = 1while Day != n:size = len(container)for _ in range(size):nodedelay, nodelife = container.popleft()if nodedelay > 1 and nodelife != 0 :container.append((nodedelay-1, nodelife-1))elif (nodedelay == 0 or nodedelay -1 == 0) and nodelife - 1 != 0:container.append((0, nodelife-1))container.append((delay, forget))Day += 1return len(container)%Mod
好了,DP版本。
class Solution:def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:dp = [0 for _ in range(1000)]dp[0] = 1for i in range(delay, n):if i < forget - 1:dp[i] = sum(dp[:i-delay+1])else:dp[i] = sum(dp[i-forget + 1:i-delay+1])return sum(dp[n-forget:])%(10**9+7)
Leetcode 2327. Number of People Aware of a Secret [Python]相关推荐
- LeetCode 191 Number of 1 Bits
LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...
- LeetCode 200. Number of Islands--c++ dfs解法
LeetCode 200. Number of Islands LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Pyt ...
- LeetCode Singer Number
Python LeetCode Singer Number 题目说明 算法1 数学方法 算法2 异或运算 题目说明 给定一个列表,其中包含数对相同元素与一个单独元素,例如[2,2,1].[1,3,5, ...
- [勇者闯LeetCode] 191. Number of 1 Bits
[勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...
- LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法
LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法
题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...
- [LeetCode]Single Number II
今天室友推荐了一道LeetCode上面的练习题Single Number II,题目大概的意思是有一个整形的数组,里面只有一个数是出现一次的,其他的数都出现了三次,然后让把这个数找出来. 第一眼看到这 ...
- leetcode - Single Number
今天开始刷leetcode上的题,争取校招前刷过一遍,从AC率最高的题目开始刷,不废话了,看题 题目:Single Number Given an array of integers, every e ...
最新文章
- 《美国如何培养核心素养》读后感作文1900字
- 虹软人脸识别Android Sample Code
- How to scroll the window using JQuery $.scrollTo() function
- SQL2008中Merge的用法
- 数据库元数据数据字典查询_2_列出所有的数据库
- oracle多条sql语句常量,如何在Oracle中一次执行多条sql语句
- 数据探查_数据科学家,开始使用探查器
- VO(DTO)模式在架构设计中是否需要
- python编程选股_用Python选一个自己的股票池2
- wk一sm5时间温度控制器_Android RTC 自下而上分析
- 在线XML转HTML工具
- Python零基础入门(三)——函数[学习笔记]
- java day09【继承、super、this、抽象类】
- python中元组和列表的区别_Python 序列:列表、元组
- 嵌入式开发 ARM Cortex-M3处理器技术优势分析
- Tableau绘制符号地图、填充地图、多维地图、混合地图
- PCB设计的工艺流程
- Win11 Windows聚焦不更新了怎么解决?聚焦锁屏图片不更换怎么办
- 习题8-5 折纸痕(Paper Folding, UVa177)
- 使用UpdateLayeredWindow有概率出现317错误解决方法
热门文章
- 跨集群复制(CCR)
- java调用金蝶云接口_调用金蝶web api
- 为什么onenote一直在加载_超好用的笔记软件,Onenote是我的最爱
- oracle查询表数据写入时间,求大神解决关于查询Oracle表数据时间
- pythonsuper继承规则,Python用super继承
- 短视频系统源代码,自定义圆盘,方向盘
- 温暖别人就是温暖自己
- android局部布局刷新,Android RecyclerView 局部刷新分析
- 直积、直和与张量积简介
- CortexM4内核Stm32-HAL_Init()及HAL_InitTick(TICK_INT_PRIORITY()详解——Systick中断优先级