Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions solution/1300-1399/1386.Cinema Seat Allocation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>>) -> i32 {
let mut d: HashMap<i32, i32> = HashMap::new();

for e in reserved_seats {
let row = e[0];
let col = e[1];
let mask = 1 << (10 - col);

d.entry(row)
Comment on lines +227 to +231
.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<int, int> d = new Dictionary<int, int>();

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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
87 changes: 87 additions & 0 deletions solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>>) -> i32 {
let mut d: HashMap<i32, i32> = HashMap::new();

for e in reserved_seats {
let row = e[0];
let col = e[1];
let mask = 1 << (10 - col);

d.entry(row)
Comment on lines +225 to +229
.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<int, int> d = new Dictionary<int, int>();

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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
41 changes: 41 additions & 0 deletions solution/1300-1399/1386.Cinema Seat Allocation/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;

public class Solution {
public int MaxNumberOfFamilies(int n, int[][] reservedSeats) {
Dictionary<int, int> d = new Dictionary<int, int>();

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;
}
}
30 changes: 30 additions & 0 deletions solution/1300-1399/1386.Cinema Seat Allocation/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::collections::HashMap;

impl Solution {
pub fn max_number_of_families(n: i32, reserved_seats: Vec<Vec<i32>>) -> i32 {
let mut d: HashMap<i32, i32> = 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
}
}
Loading