You are given two positive integers startPos and endPos. Initially, you are standing at position startPos on an infinite number line. With one step, you can move either one position to the left, or one position to the right.
Given a positive integer k, return _the number ofdifferent ways to reach the position _endPosstarting fromstartPos _, such that you performexactly _ksteps. Since the answer may be very large, return it
modulo10^9 + 7.
Two ways are considered different if the order of the steps made is not exactly the same.
Note that the number line includes negative integers.
Input: startPos =1, endPos =2, k =3Output: 3Explanation: We can reach position 2 from 1in exactly 3 steps in three ways:-1->2->3->2.-1->2->1->2.-1->0->1->2.It can be proven that no other way is possible, so we return3.
Each step can be left or right. To reach endPos from startPos in k steps, the difference d = abs(endPos - startPos) must have the same parity as k and d <= k. The number of ways is C(k, (k+d)//2), where (k+d)//2 is the number of right moves.
classSolution {
public:int numberOfWays(int startPos, int endPos, int k) {
constint mod =1e9+7;
int d = abs(endPos - startPos);
if (d > k || (k-d)%2) return0;
int r = (k+d)/2;
vector<longlong> fact(k+1, 1), inv(k+1, 1);
for (int i =1; i <= k; ++i) fact[i] = fact[i-1]*i%mod;
inv[k] =1;
for (int i = k-1; i >=0; --i) inv[i] = inv[i+1]*(i+1)%mod;
return fact[k]*inv[r]%mod*inv[k-r]%mod;
}
};
classSolution {
publicintnumberOfWays(int startPos, int endPos, int k) {
int mod = 1_000_000_007;
int d = Math.abs(endPos - startPos);
if (d > k || (k-d)%2 != 0) return 0;
int r = (k+d)/2;
long[] fact =newlong[k+1], inv =newlong[k+1];
fact[0]= 1;
for (int i = 1; i <= k; i++) fact[i]= fact[i-1]*i%mod;
inv[k]= 1;
for (int i = k-1; i >= 0; i--) inv[i]= inv[i+1]*(i+1)%mod;
return (int)(fact[k]*inv[r]%mod*inv[k-r]%mod);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funnumberOfWays(startPos: Int, endPos: Int, k: Int): Int {
val mod = 1_000_000_007
val d = kotlin.math.abs(endPos - startPos)
if (d > k || (k-d)%2!=0) return0val r = (k+d)/2val fact = LongArray(k+1) { 1L }
val inv = LongArray(k+1) { 1L }
for (i in1..k) fact[i] = fact[i-1]*i%mod
inv[k] = 1Lfor (i in k-1 downTo 0) inv[i] = inv[i+1]*(i+1)%mod
return (fact[k]*inv[r]%mod*inv[k-r]%mod).toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defnumberOfWays(self, startPos: int, endPos: int, k: int) -> int:
mod =10**9+7 d = abs(endPos - startPos)
if d > k or (k-d)%2:
return0 r = (k+d)//2 fact = [1]*(k+1)
for i in range(1, k+1):
fact[i] = fact[i-1]*i%mod
inv = [1]*(k+1)
inv[k] = pow(fact[k], mod-2, mod)
for i in range(k-1, -1, -1):
inv[i] = inv[i+1]*(i+1)%mod
return fact[k]*inv[r]%mod*inv[k-r]%mod
impl Solution {
pubfnnumber_of_ways(start_pos: i32, end_pos: i32, k: i32) -> i32 {
let d = (end_pos - start_pos).abs();
let k = k asusize;
let d = d asusize;
let modulo =1_000_000_007;
if d > k || (k-d)%2!=0 { return0; }
let r = (k+d)/2;
letmut fact =vec![1i64; k+1];
for i in1..=k { fact[i] = fact[i-1]*i asi64%modulo; }
letmut inv =vec![1i64; k+1];
inv[k] = pow(fact[k], modulo-2, modulo);
for i in (0..k).rev() { inv[i] = inv[i+1]*(i asi64+1)%modulo; }
(fact[k]*inv[r]%modulo*inv[k-r]%modulo) asi32 }
}
fnpow(mut a: i64, mut b: i64, m: i64) -> i64 {
letmut res =1i64;
while b >0 {
if b&1==1 { res = res*a%m; }
a = a*a%m; b >>=1;
}
res
}