1045. Sequence Reconstruction Using Topo Sort

MediumGraphsTopological SortGraph

Given a permutation 'nums' of 1..n and a list of subsequences 'sequences', determine whether nums is the unique shortest sequence consistent with all the subsequences (i.e. the only valid topological order). Return true or false. The input is JSON {nums, sequences}.

Input: JSON {nums, sequences}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"nums":[1,2,3],"sequences":[[1,2],[1,3],[2,3]]}
Output: true
Explanation: Order uniquely forced.
Example 2
Input: {"nums":[1,2,3],"sequences":[[1,2],[1,3]]}
Output: false
Explanation: 2 and 3 order not fixed.
Example 3
Input: {"nums":[1],"sequences":[[1]]}
Output: true
Explanation: Single element.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →