LeetCode Entry

1386. Cinema Seat Allocation

19.08.2026 medium 2026 kotlin rust

Max non-excluded 4-groups in n rows

1386. Cinema Seat Allocation medium substack youtube

https://dmitrysamoylenko.com/leetcode/

19.08.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1456

Problem TLDR

Max non-excluded 4-groups in n rows

Intuition

Total max groups are 2*n. Exclude groups by comparing bitmasks.

Approach

  • we can safely use sum() instead of OR

Complexity

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

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

Code

    fun maxNumberOfFamilies(n: Int, rs: Array<IntArray>) = n * 2 -
    rs.groupBy({ it[0] }) { 1 shl it[1] }.values.sumOf { v ->
        2 - (setOf(60, 960, 240).count { v.sum() and it == 0 } + 1) / 2
    }
    pub fn max_number_of_families(n: i32, rs: Vec<Vec<i32>>) -> i32 {
        n * 2 - rs.into_iter().map(|s| (s[0], 1 << s[1])).into_group_map().values().map(|v| {
            let m = v.iter().sum::<i32>();
            2 - (m & 1020 == 0) as i32 - ((m & 60) * (m & 960) * (m & 240) == 0) as i32
        }).sum::<i32>()
    }

Comments