LeetCode Entry

1791. Find Center of Star Graph

27.06.2024 easy 2024 kotlin rust

Center of a start graph

1791. Find Center of Star Graph easy blog post substack youtube 2024-06-27_06-48_1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/652

Problem TLDR

Center of a start graph #easy

Intuition

It’s just a common node between two edges.

Approach

Can you make it shorter?

Complexity

  • Time complexity: \(O(1)\)

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

Code


    fun findCenter(e: Array<IntArray>) =
        e[0].first { it in e[1] }


    pub fn find_center(e: Vec<Vec<i32>>) -> i32 {
       if e[1].contains(&e[0][0]) { e[0][0] } else { e[0][1] }
    }