diff --git a/solution/1300-1399/1386.Cinema Seat Allocation/README.md b/solution/1300-1399/1386.Cinema Seat Allocation/README.md index 6ecc7bbff6d87..f27a1262ec48b 100644 --- a/solution/1300-1399/1386.Cinema Seat Allocation/README.md +++ b/solution/1300-1399/1386.Cinema Seat Allocation/README.md @@ -214,6 +214,93 @@ function maxNumberOfFamilies(n: number, reservedSeats: number[][]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_number_of_families(n: i32, reserved_seats: Vec>) -> i32 { + let mut d: HashMap = HashMap::new(); + + for e in reserved_seats { + let row = e[0]; + let col = e[1]; + let mask = 1 << (10 - col); + + d.entry(row) + .and_modify(|x| *x |= mask) + .or_insert(mask); + } + + let masks = [ + 0b0111100000, + 0b0000011110, + 0b0001111000, + ]; + + let mut ans = (n - d.len() as i32) * 2; + + for mut x in d.values().copied() { + for &mask in &masks { + if (x & mask) == 0 { + x |= mask; + ans += 1; + } + } + } + + ans + } +} +``` + +#### C# + +```cs +using System; +using System.Collections.Generic; + +public class Solution { + public int MaxNumberOfFamilies(int n, int[][] reservedSeats) { + Dictionary d = new Dictionary(); + + foreach (var e in reservedSeats) { + int row = e[0]; + int col = e[1]; + int mask = 1 << (10 - col); + + if (d.ContainsKey(row)) { + d[row] |= mask; + } else { + d[row] = mask; + } + } + + int[] masks = { + 0b0111100000, + 0b0000011110, + 0b0001111000 + }; + + int ans = (n - d.Count) * 2; + + foreach (int value in d.Values) { + int x = value; + + foreach (int mask in masks) { + if ((x & mask) == 0) { + x |= mask; + ans++; + } + } + } + + return ans; + } +} +``` + diff --git a/solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md b/solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md index e97bf1a189d40..9687a818404ca 100644 --- a/solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md +++ b/solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md @@ -212,6 +212,93 @@ function maxNumberOfFamilies(n: number, reservedSeats: number[][]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn max_number_of_families(n: i32, reserved_seats: Vec>) -> i32 { + let mut d: HashMap = HashMap::new(); + + for e in reserved_seats { + let row = e[0]; + let col = e[1]; + let mask = 1 << (10 - col); + + d.entry(row) + .and_modify(|x| *x |= mask) + .or_insert(mask); + } + + let masks = [ + 0b0111100000, + 0b0000011110, + 0b0001111000, + ]; + + let mut ans = (n - d.len() as i32) * 2; + + for mut x in d.values().copied() { + for &mask in &masks { + if (x & mask) == 0 { + x |= mask; + ans += 1; + } + } + } + + ans + } +} +``` + +#### C# + +```cs +using System; +using System.Collections.Generic; + +public class Solution { + public int MaxNumberOfFamilies(int n, int[][] reservedSeats) { + Dictionary d = new Dictionary(); + + foreach (var e in reservedSeats) { + int row = e[0]; + int col = e[1]; + int mask = 1 << (10 - col); + + if (d.ContainsKey(row)) { + d[row] |= mask; + } else { + d[row] = mask; + } + } + + int[] masks = { + 0b0111100000, + 0b0000011110, + 0b0001111000 + }; + + int ans = (n - d.Count) * 2; + + foreach (int value in d.Values) { + int x = value; + + foreach (int mask in masks) { + if ((x & mask) == 0) { + x |= mask; + ans++; + } + } + } + + return ans; + } +} +``` + diff --git a/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cs b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cs new file mode 100644 index 0000000000000..998693dccff6a --- /dev/null +++ b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; + +public class Solution { + public int MaxNumberOfFamilies(int n, int[][] reservedSeats) { + Dictionary d = new Dictionary(); + + foreach (var e in reservedSeats) { + int row = e[0]; + int col = e[1]; + int mask = 1 << (10 - col); + + if (d.ContainsKey(row)) { + d[row] |= mask; + } else { + d[row] = mask; + } + } + + int[] masks = { + 0b0111100000, + 0b0000011110, + 0b0001111000 + }; + + int ans = (n - d.Count) * 2; + + foreach (int value in d.Values) { + int x = value; + + foreach (int mask in masks) { + if ((x & mask) == 0) { + x |= mask; + ans++; + } + } + } + + return ans; + } +} \ No newline at end of file diff --git a/solution/1300-1399/1386.Cinema Seat Allocation/Solution.rs b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.rs new file mode 100644 index 0000000000000..055393e20df04 --- /dev/null +++ b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.rs @@ -0,0 +1,30 @@ +use std::collections::HashMap; + +impl Solution { + pub fn max_number_of_families(n: i32, reserved_seats: Vec>) -> i32 { + let mut d: HashMap = HashMap::new(); + + for e in reserved_seats { + let row = e[0]; + let col = e[1]; + let mask = 1 << (10 - col); + + d.entry(row).and_modify(|x| *x |= mask).or_insert(mask); + } + + let masks = [0b0111100000, 0b0000011110, 0b0001111000]; + + let mut ans = (n - d.len() as i32) * 2; + + for mut x in d.values().copied() { + for &mask in &masks { + if (x & mask) == 0 { + x |= mask; + ans += 1; + } + } + } + + ans + } +}