Grace Hopper is widely known as the "First Lady of Software." She is primarily known for helping to create the first all-electrical digital computer: UNIVAC (Universal Automatic Computer). NASA's tracking stations for the Apollo moon missions used UNIVAC hardware to communicate with the astronauts.
While developing UNIVAC, one of Hopper's tasks was to come up with a process synchronization solution for ensuring that the never-ending processes must suffer a bounded wait. A never-ending process is defined as a process that never completes. For this, she devised an algorithm wherein one process cannot occupy consecutive time slots.
To estimate the algorithm performance, Hopper needed to find the number of ways to allocate
n_processes
in n_intervals
at different time intervals by obeying the algorithm.
Since the number of ways can be very large, return the answer in modulo 10^9+7.
Function Description
Complete the function findNumberOfWays
in the editor below.
findNumberOfWays
has the following parameters:
n_intervals
: an integer representing distinct time intervalsn_processes
: an integer representing the number of processes
Returns
int: an integer denoting the number of ways processes can be scheduled in time intervals modulo 10^9+7.
Example
Suppose that n_processes = 2
and n_intervals = 3
. The schedule allocation
can look like {A, B, A} or {B, A, B}.
Note: all the processes are never-ending, so even if n_intervals
are very large, the processes will not be complete.
Example 1:
Input: n_intervals = 3, n_processes = 2
Output: 2
Explanation:The schedule allocation can look like {A, B, A} or {B, A, B}.
- 1 ≤
n_intervals
≤ 10^3 - 1 ≤
n_processes
≤ 10^3

input:
output: