java.lang.Object
g2001_2100.s2076_process_restricted_friend_requests.Solution

public class Solution extends Object
2076 - Process Restricted Friend Requests.<p>Hard</p> <p>You are given an integer <code>n</code> indicating the number of people in a network. Each person is labeled from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a <strong>0-indexed</strong> 2D integer array <code>restrictions</code>, where <code>restrictions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> means that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> <strong>cannot</strong> become <strong>friends</strong> , either <strong>directly</strong> or <strong>indirectly</strong> through other people.</p> <p>Initially, no one is friends with each other. You are given a list of friend requests as a <strong>0-indexed</strong> 2D integer array <code>requests</code>, where <code>requests[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> is a friend request between person <code>u<sub>j</sub></code> and person <code>v<sub>j</sub></code>.</p> <p>A friend request is <strong>successful</strong> if <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> can be <strong>friends</strong>. Each friend request is processed in the given order (i.e., <code>requests[j]</code> occurs before <code>requests[j + 1]</code>), and upon a successful request, <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> <strong>become direct friends</strong> for all future friend requests.</p> <p>Return <em>a <strong>boolean array</strong></em> <code>result</code>, <em>where each</em> <code>result[j]</code> <em>is</em> <code>true</code> <em>if the</em> <code>j<sup>th</sup></code> <em>friend request is <strong>successful</strong> or</em> <code>false</code> <em>if it is not</em>.</p> <p><strong>Note:</strong> If <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> are already direct friends, the request is still <strong>successful</strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]</p> <p><strong>Output:</strong> [true,false]</p> <p><strong>Explanation:</strong></p> <p>Request 0: Person 0 and person 2 can be friends, so they become direct friends.</p> <p>Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1&ndash;2&ndash;0).</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]</p> <p><strong>Output:</strong> [true,false]</p> <p><strong>Explanation:</strong></p> <p>Request 0: Person 1 and person 2 can be friends, so they become direct friends.</p> <p>Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0&ndash;2&ndash;1).</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]</p> <p><strong>Output:</strong> [true,false,true,false]</p> <p><strong>Explanation:</strong></p> <p>Request 0: Person 0 and person 4 can be friends, so they become direct friends.</p> <p>Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.</p> <p>Request 2: Person 3 and person 1 can be friends, so they become direct friends.</p> <p>Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0&ndash;4&ndash;3&ndash;1).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 1000</code></li> <li><code>0 <= restrictions.length <= 1000</code></li> <li><code>restrictions[i].length == 2</code></li> <li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code></li> <li><code>x<sub>i</sub> != y<sub>i</sub></code></li> <li><code>1 <= requests.length <= 1000</code></li> <li><code>requests[j].length == 2</code></li> <li><code>0 <= u<sub>j</sub>, v<sub>j</sub> <= n - 1</code></li> <li><code>u<sub>j</sub> != v<sub>j</sub></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • friendRequests

      public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests)