Skip to content

Commit 10f57a4

Browse files
committed
feat(leetcode/easy/603-consecutive-available-seats.py)
1 parent 6bc341c commit 10f57a4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# leetcode/easy/603. Consecutive Available Seats
2+
# 603-consecutive-available-seats
3+
# URL: https://leetcode.com/problems/consecutive-available-seats/
4+
5+
import pandas as pd
6+
7+
8+
def consecutive_available_seats(dataFrame: pd.DataFrame) -> pd.DataFrame:
9+
# Identify where the value changes
10+
dataFrame['change'] = dataFrame['free'].diff().ne(0).cumsum()
11+
print(dataFrame)
12+
# seat_id free change
13+
# 0 1 1 1
14+
# 1 2 0 2
15+
# 2 3 1 3
16+
# 3 4 1 3
17+
# 4 5 1 3
18+
19+
# Filter for free seats and group by consecutive free seats
20+
free_seats = dataFrame[dataFrame['free'] == 1].groupby('change')
21+
print(free_seats)
22+
23+
# Get rows of groups with more than one member
24+
consecutive_free_seats = free_seats.filter(lambda x: len(x) > 1)
25+
print(consecutive_free_seats)
26+
# seat_id free change
27+
# 2 3 1 3
28+
# 3 4 1 3
29+
# 4 5 1 3
30+
31+
# Drop the 'change' column as it's no longer needed
32+
consecutive_free_seats = consecutive_free_seats.drop(columns=['change', 'free'])
33+
print(consecutive_free_seats)
34+
# seat_id
35+
# 2 3
36+
# 3 4
37+
# 4 5
38+
consecutive_free_seats = consecutive_free_seats.sort_values(by=['seat_id'])
39+
40+
return consecutive_free_seats

0 commit comments

Comments
 (0)