-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
36 lines (28 loc) · 932 Bytes
/
Copy pathsolution.java
File metadata and controls
36 lines (28 loc) · 932 Bytes
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
class Solution {
public boolean canSeatAllPeople(int k, int[] seats) {
int n = seats.length;
// No people need seating
if (k == 0)
return true;
// Traverse all seats
for (int i = 0; i < n; i++) {
// Skip occupied seats
if (seats[i] == 1)
continue;
// Check left side
boolean leftEmpty = (i == 0 || seats[i - 1] == 0);
// Check right side
boolean rightEmpty = (i == n - 1 || seats[i + 1] == 0);
// Safe position found
if (leftEmpty && rightEmpty) {
seats[i] = 1; // Occupy seat
k--; // One person seated
// All people seated
if (k == 0)
return true;
}
}
// Could not seat everyone
return false;
}
}