Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check whether all can get a seat or not in Python
Suppose we have a number n, there are n number of people searching for a seat, we also have a list of bits where 1 represents an already occupied seat and 0 represents empty seat. No two people can sit next to each other, so we have to check whether all n people can find a seat or not.
So, if the input is like n = 2 seats = [1, 0, 0, 0, 1, 0, 0], then the output will be True, because they can seat at index 2 and 6.
Approach
To solve this, we will follow these steps ?
- Insert 0 at beginning of seats and insert [0, 1] at the end of seats
- Initialize res := 0, gap := 0
- For each i in seats, do
- If i is same as 0, then
- gap := gap + 1
- Otherwise when gap > 0, then
- res := res + floor of (gap − 1)/2
- gap := 0
- If i is same as 0, then
- Return true when res >= n otherwise false
How It Works
The algorithm adds padding (0 at start, [0, 1] at end) to handle edge cases. It counts consecutive empty seats (gaps) and calculates how many people can sit in each gap using the formula (gap-1)//2, since people need one empty seat between them.
Example
Let us see the following implementation to get better understanding ?
def solve(n, seats):
seats = [0] + seats + [0, 1]
res = 0
gap = 0
for i in seats:
if i == 0:
gap += 1
elif gap > 0:
res += (gap - 1) // 2
gap = 0
return res >= n
n = 2
seats = [1, 0, 0, 0, 1, 0, 0]
print(solve(n, seats))
The output of the above code is ?
True
Step-by-Step Execution
For the example seats = [1, 0, 0, 0, 1, 0, 0] ?
def solve_with_steps(n, seats):
print(f"Original seats: {seats}")
seats = [0] + seats + [0, 1]
print(f"Modified seats: {seats}")
res = 0
gap = 0
for i, seat in enumerate(seats):
if seat == 0:
gap += 1
print(f"Position {i}: Empty seat, gap = {gap}")
elif gap > 0:
available = (gap - 1) // 2
res += available
print(f"Position {i}: Occupied seat, can place {available} people from gap {gap}")
gap = 0
print(f"Total available seats: {res}, People needed: {n}")
return res >= n
n = 2
seats = [1, 0, 0, 0, 1, 0, 0]
result = solve_with_steps(n, seats)
print(f"Can all {n} people get seats? {result}")
Original seats: [1, 0, 0, 0, 1, 0, 0] Modified seats: [0, 1, 0, 0, 0, 1, 0, 0, 0, 1] Position 0: Empty seat, gap = 1 Position 2: Empty seat, gap = 1 Position 3: Empty seat, gap = 2 Position 4: Empty seat, gap = 3 Position 5: Occupied seat, can place 1 people from gap 3 Position 6: Empty seat, gap = 1 Position 7: Empty seat, gap = 2 Position 8: Empty seat, gap = 3 Position 9: Occupied seat, can place 1 people from gap 3 Total available seats: 2, People needed: 2 Can all 2 people get seats? True
Conclusion
This algorithm efficiently determines if n people can be seated with social distancing by counting gaps between occupied seats. The key insight is that in a gap of k empty seats, we can place (k-1)//2 people with proper spacing.
