当前位置:网站首页>LeetCode LCP 07. 传递信息
LeetCode LCP 07. 传递信息
2022-06-12 01:38:00 【Alex_996】
题目链接:LCP 07. 传递信息
Ideas
算法:深度优先搜索
数据结构:图
思路:题目中要求的是所有的方案数,自然而然的想到要用深度优先搜索DFS,通过k来控制搜索的深度即可。
Code
Python
class Solution:
def numWays(self, n: int, relation: List[List[int]], k: int) -> int:
def dfs(idx, depth):
if depth > k:
return
if depth == k and idx == n - 1:
nonlocal ans
ans += 1
for target in adjacency.get(idx, []):
dfs(target, depth + 1)
adjacency = {
}
for (start, end) in relation:
if adjacency.get(start, None):
adjacency[start].append(end)
else:
adjacency[start] = [end]
ans = 0
dfs(0, 0)
return ans
边栏推荐
- [C language] C language file operation | C language file reading and writing | single character reading and writing | string reading and writing | format reading and writing | binary form reading and
- 【项目实训】微信公众号模板消息推送
- Henan Zhongchuang - from cloud to edge, how edge computing enables data centers
- New knowledge: monkey improved app crawler
- Machines / scenarios not suitable for webrdp
- These three sentences make you not confused when you understand that you are an we media. I just do this. The monthly income of video has exceeded 10000 yuan
- Global and Chinese lutetium oxide powder industry investigation and analysis and Investment Strategy Research Report 2022-2028
- 华为,这也太强了吧..
- 括号生成(回溯)
- Loop loop and CX
猜你喜欢

Huawei, this is too strong

Prism框架初识-模块化介绍

"C'est plus sûr d'apprendre une technologie!" Hangzhou Campus Little Brother transfer software test, Hi - Ti 10K + double break!

MySQL实训报告【带源码】

Esp8266wifi development board collects temperature and humidity data and uploads them to the Internet of things platform

kmeans从0到1

华为,这也太强了吧..

Annotate your own point cloud dataset with labelcloud open source tool as a tutorial of Kitti annotation format (support PCD and bin point clouds)

Sogou Pinyin official website screenshot tool pycharm installation

Markov networks and conditional random fields
随机推荐
"Xilin chain" of Southwest Forestry University passed the functional test of Electronic Standards Institute of the Ministry of industry and information technology | FISCO bcos case
括号生成(回溯)
The road of global evolution of vivo global mall -- multilingual solution
In depth description of Weibull distribution (1) principle and formula
Agile v.s. Waterfall
"It's safer to learn a skill!" The little brother of Hangzhou campus changes to software testing, and likes to mention 10k+ weekend!
颠倒字符串中的单词(split、双端队列)
Blog recommended | bookkeeper - Apache pulsar high availability / strong consistency / low latency storage implementation
[C language] summary of basic knowledge points of pointer
Why should a redis cluster use a reverse proxy? Just read this one
Watermelon video synchronization Tiktok also has benefits ~ the official "China Video Partner Program"
[project training] JWT
New knowledge: monkey improved app crawler
UoE UG2 Inf Course Research
Make good use of these 28 tools, and the development efficiency soars
Three line code solution - Maximum sub segment and - DP
Matlab 基础应用02 wind 股票数据介绍和使用案例:
In depth description of Weibull distribution (2) meaning of parameters and formulas
一看就懂的JMeter操作流程
Chinese Version Vocaloid AI Tuner Feasibility Test