File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
coding-challange/leetcode/easy/603-consecutive-available-seats Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments