-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
41 lines (33 loc) · 1.02 KB
/
Copy pathsolution.cpp
File metadata and controls
41 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution
{
public:
bool canSeatAllPeople(int k, vector<int> &seats)
{
int n = seats.size();
// No people need to be seated
if (k == 0)
return true;
// Check every seat once
for (int i = 0; i < n; i++)
{
// Skip if already occupied
if (seats[i] == 1)
continue;
// Check left neighbor
bool leftEmpty = (i == 0 || seats[i - 1] == 0);
// Check right neighbor
bool rightEmpty = (i == n - 1 || seats[i + 1] == 0);
// If both sides are safe, seat a person here
if (leftEmpty && rightEmpty)
{
seats[i] = 1; // Mark seat as occupied
k--; // One person seated
// If everyone is seated, return true
if (k == 0)
return true;
}
}
// Not enough valid seats found
return false;
}
};