Skip to content

Commit 0b8d673

Browse files
authored
Added solution for 2147
1 parent 3abe4d4 commit 0b8d673

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
impl Solution {
2+
pub fn number_of_ways(corridor: String) -> i32 {
3+
// Loop through corridor and calculate number of seats. If there are only two seats, return 1, if the total seats are not even-numbered or no seats exist, there is no possible solution because they can't be divided into groups of 2. If seats are even, maintain a vector which keeps track of differences of every odd and even numbered seat (excluding first one). At the end, multiply all these numbers in the vector, because each of these numbers represent the combinations possible of placing a corridor between those pairs of seats, and the final solution would be a product of all these combinations
4+
let mut seats_count = 0;
5+
let mut second_seat_position = 0;
6+
let mut third_seat_position = 0;
7+
let mut vec = vec![];
8+
let mut output: i64 = 1;
9+
for (index, item) in corridor.chars().enumerate() {
10+
if item == 'S' {
11+
seats_count += 1;
12+
if seats_count % 2 == 0 {
13+
second_seat_position = index;
14+
}
15+
else if (second_seat_position != 0) && (seats_count % 2 == 1) {
16+
third_seat_position = index;
17+
vec.push(third_seat_position - second_seat_position);
18+
}
19+
}
20+
}
21+
22+
if seats_count == 2 {
23+
return 1;
24+
}
25+
if (seats_count % 2) != 0 || seats_count == 0 {
26+
return 0;
27+
}
28+
29+
for i in vec {
30+
output *= (i as i64);
31+
output %= 1000000007;
32+
}
33+
34+
return output as i32;
35+
}
36+
}

0 commit comments

Comments
 (0)