LeetCode 685. Redundant Connection II

原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/

题目:

In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] that represents a directed edge connecting nodes u and v, where u is a parent of child v.

Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.

Example 1:

Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given directed graph will be like this:
  1
 / v   v
2-->3

Example 2:

Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
Output: [4,1]
Explanation: The given directed graph will be like this:
5 <- 1 -> 2
     ^    |
     |    v
     4 <- 3

Note:

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

题解:

If it is a invalid tree, there could be 2 cases:

  • one node has 2 parents. [i, j], [k, j], two edges both point to j.
  • there is cyrcle.

If remove one redundant edge could make it a valid tree.

If case 1 happens, then redundant edge must be either [i, j] or [k, j]. Otherwise, even you remove the redundant edge, [i, j] and [k, j] still point to the same node j and it is still invalid. Thus make them candidate 1 and candidate 2.

We check if cycle exists, if it exists, we check if case 1 happens or not. If no, then edge contecting 2 nodes already within the same union is the redundant edge likeRedundant Connection.

If yes, redundant edge is either candiate 1 or 2. First remove candidate 2 and check if cycle still exists, if no then answer is candidate 2, otherwise it is candidate 1.

Time Complexity: O(nlogn). find takes O(n).

Space: O(n).

AC Java:

 1 class Solution {
 2     int [] parent;
 3
 4     public int[] findRedundantDirectedConnection(int[][] edges) {
 5         int n = edges.length;
 6         parent = new int[n+1];
 7
 8         int [] can1 = new int[]{-1, -1};
 9         int [] can2 = new int[]{-1, -1};
10         for(int i = 0; i<n; i++){
11             if(parent[edges[i][1]] == 0){
12                 parent[edges[i][1]] = edges[i][0];
13             }else{
14                 can2 = new int[]{edges[i][0], edges[i][1]};
15                 can1 = new int[]{parent[edges[i][1]], edges[i][1]};
16                 edges[i][1] = 0;
17             }
18         }
19
20         for(int i = 0; i<=n; i++){
21             parent[i] = i;
22         }
23
24         for(int [] edge : edges){
25             if(find(edge[0]) == find(edge[1])){
26                 if(can1[0] == -1){
27                     return edge;
28                 }
29
30                 return can1;
31             }
32
33             union(edge[0], edge[1]);
34         }
35
36         return can2;
37     }
38
39     private int find(int i){
40         if(i != parent[i]){
41             parent[i] = find(parent[i]);
42         }
43
44         return parent[i];
45     }
46
47     private void union(int i, int j){
48         int p = find(i);
49         int q = find(j);
50         parent[q] = p;
51     }
52 }

类似Redundant Connection.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11237192.html

时间: 2024-10-09 16:39:31

LeetCode 685. Redundant Connection II的相关文章

LN : leetcode 684 Redundant Connection

lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one addi

[Swift]LeetCode684. 冗余连接 | Redundant Connection

In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different v

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following