Skip to content

Commit

Permalink
Sync LeetCode submission - Number of Ways to Divide a Long Corridor (…
Browse files Browse the repository at this point in the history
…javascript)
  • Loading branch information
DhanushNehru committed Nov 28, 2023
1 parent 6ab844c commit 2ee7ae4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions problems/number_of_ways_to_divide_a_long_corridor/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {string} corridor
* @return {number}
*/
// Dynamic programming
var numberOfWays = function(corridor) {
// a the number of 0 seat
// b the number of 1 seat
// c the number of 2 seats
let a = 1, b = 0, c = 0, mod = 1e9 + 7;

for (let i = 0; i < corridor.length; ++i) {
a = (a + c) % mod;

if (corridor.charAt(i) === 'S') {
c = b;
b = a;
a = 0;
}
}

return c;
};

0 comments on commit 2ee7ae4

Please sign in to comment.