当前位置:网站首页>Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
2022-07-26 03:11:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/ur2n8P/solution/by-xun-ge-v-nayf/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject

Example

Ideas
Their thinking
A topological sort
take sequences Set as a directed graph , subject -> Determine whether there is a unique path in the directed graph
such as sequences = [1,3][1,4][2,3] When we choose 1 For the population of this digraph , The degree of 0 Yes 3, and 4( Is to choose 1 As the population of the graph , Yes 3,4 Two directions can be selected ), Then there are two ways , It's not the only one , return false that will do , The problem requires that when traversing a directed graph, there is always only one direction , That is, always keep the degree of penetration of the entire directed graph as 0 The only element of exists .
Concrete realization
Obviously, topological sorting is needed here , Define an array ans Initialize to 0, take sequences The penetration of all elements in are counted and recorded in ans in , The size of the whole graph is numsSize, all ans The size is also numsSize+1, When directed graph sequences Does not exist in the nums Some elements in , It can be understood that a directed graph will have multiple entries , It will definitely not meet the requirements .
Code
bool sequenceReconstruction(int* nums, int numsSize, int** sequences, int sequencesSize, int* sequencesColSize){
int ans[numsSize+1];// Initialization penetration
int queue[2];// The degree of recording is 0 The elements of , Because the topic requires to keep 1 individual , It doesn't need to be big
memset(ans, 0, sizeof(int) * (numsSize+1));
for(int i = 0; i < sequencesSize; i++)// Calculate all in degrees
{
for(int j = 1; j < sequencesColSize[i]; j++)
{
ans[sequences[i][j]]++;
}
}
int node = 0;
for(int i = 1; i <= numsSize; i++)// The search degree is 0 The elements of
{
if(ans[i] == 0)
{
queue[node++] = i;
if(node == 2)// Degree is 0 There are two situations , Not meeting the requirements
{
return false;
}
}
}
while(node)// As long as the degree of existence is 0 The situation of , It's been circulating
{
node = 0;// Reset the degree to 0 Of the team
int mm = queue[0];
for(int i = 0; i < sequencesSize; i++)
{
for(int j = 0; j < sequencesColSize[i] - 1; j++)
{
if(sequences[i][j] == mm)// take mm Points to the next element degree minus 1, because sequences[i][j] It's been dealt with
{
ans[sequences[i][j+1]]--;
if(ans[sequences[i][j+1]] == 0)// Degree is 0 Just line up , Waiting to be dealt with
{
queue[node++] = ans[sequences[i][j+1]];
if(node == 2)
{
return false;
}
}
}
}
}
}
return true;
}
author :xun-ge-v
link :https://leetcode.cn/problems/ur2n8P/solution/by-xun-ge-v-nayf/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .Time and space complexity

边栏推荐
- 在混合云中管理数据库:八个关键注意事项
- Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
- Win11 hide input method status bar method
- 朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
- [translation] cloud like internal load balancer for kubernetes?
- OxyCon 2022 网络抓取前沿大会即将开启!
- 文件操作(一)——文件简介与文件的打开方式和关闭
- Usage of '...' in golang
- STM——EXTI外部中断学习笔记
- 一篇文章让你理解 云原生 容器化相关
猜你喜欢

Oxycon 2022 network capture frontier conference is about to open!

万维网、因特网和互联网的区别

Parallelloopbody in opencv

Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection

c语言分层理解(c语言函数)

There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first

图解LeetCode——5. 最长回文子串(难度:中等)

STM——EXTI外部中断学习笔记

Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered

对于稳定性测试必需关注的26点
随机推荐
Type the URL to the web page display. What happened during this period?
Opening method of win11 microphone permission
canvas——矩形的绘制——柱状图的制作
Arthas' dynamic load class (retransform)
Swin Transformer【Backbone】
当点击Play以后,EditorWindow中的变量会被莫名其妙销毁.
Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
An article allows you to understand the relevance of cloud native containerization
Installation and operation of orb-slam2 under ROS
一切的源头,代码分支策略的选择
Teach you to rely on management hand in hand
手把手教你依赖管理
[NOIP2001 普及组] 最大公约数和最小公倍数问题
持续交付和DevOps是一对好基友
【C进阶】深入探索数据的存储(深度剖析+典例解读)
STM32 - DMA notes
中国信通院陈屹力:降本增效是企业云原生应用的最大价值
STM32——DMA笔记
Arthas download and startup
文件操作(一)——文件简介与文件的打开方式和关闭