LeetCode Entry

2076. Process Restricted Friend Requests

29.11.2026 hard 2026 kotlin rust

Can friends connect without connecting enemies

2076. Process Restricted Friend Requests hard substack youtube

https://dmitrysamoylenko.com/leetcode/

29.11.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1557

Problem TLDR

Can friends connect without connecting enemies

Intuition

Use Union-Find to connect friends. Check each enemy pair before.

Approach

  • we can use Quick-Find instead: store ids of groups directly, rewrite them in O(n)

Complexity

  • Time complexity: \(O(n^2)\)

  • Space complexity: \(O(n)\)

Code

    fun friendRequests(n: Int, r: Array<IntArray>, q: Array<IntArray>)=IntArray(n){it}
    .let{u->q.map { (qa,qb) -> val a = u[qa]; val b = u[qb]
            a==b||r.all { (c, d) -> val rc = u[c]; val rd = u[d]
                (a != rc || b != rd) && (a != rd || b != rc)
            } && run{for (i in u.indices) if (u[i] == a) u[i] = b;true}
        }}
    pub fn friend_requests(n: i32, r: Vec<Vec<i32>>, q: Vec<Vec<i32>>) -> Vec<bool> {
        let mut u: Vec<_> = (0..n).collect();
        q.into_iter().map(|x| {
            let (a, b) = (u[x[0] as usize], u[x[1] as usize]);
            r.iter().all(|y| { let (c, d) = (u[y[0] as usize], u[y[1] as usize]); (a, b) != (c, d) && (a, b) != (d, c) })
                && { for v in &mut u { if *v == a { *v = b } } true }
        }).collect()
    }

Comments