algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

cinema-seat-allocation-v4.py (1056B)


      1 # ISSUE: Only need to track bitmask for items that have elements
      2 class Solution:
      3     def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
      4         bitmask = []
      5 
      6         for i in range(n):
      7             # leftmost and rightmost don't matter
      8             bitmask.append(0b00000000)
      9         
     10         for seat in reservedSeats:
     11             if seat[1] != 10 and seat[1] != 1:
     12                 bitmask[seat[0] - 2] = bitmask[seat[0] - 2] | 2**(seat[1] - 2)
     13         
     14         mask_first = 0b11110000
     15         mask_second =0b00111100
     16         mask_third = 0b00001111
     17 
     18         count = 0
     19 
     20         for i in range(n):
     21             current = bitmask[i]
     22 
     23             first = False
     24             second = False
     25 
     26 
     27             if current & mask_first == 0:
     28                 count += 1
     29                 first = True
     30             
     31             if not first and current & mask_second == 0:
     32                 count += 1
     33                 second = True
     34             
     35             if not second and current & mask_third == 0:
     36                 count += 1
     37 
     38         return count