2400. Number of Ways to Reach a Position After Exactly k Steps Medium
1/**
2 * [2400] Number of Ways to Reach a Position After Exactly k Steps
3 *
4 * 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.
5 * Given a positive integer k, return the number of different ways to reach the position endPos starting from startPos, such that you perform exactly k steps. Since the answer may be very large, return it modulo 10^9 + 7.
6 * Two ways are considered different if the order of the steps made is not exactly the same.
7 * Note that the number line includes negative integers.
8 *
9 * Example 1:
10 *
11 * Input: startPos = 1, endPos = 2, k = 3
12 * Output: 3
13 * Explanation: We can reach position 2 from 1 in exactly 3 steps in three ways:
14 * - 1 -> 2 -> 3 -> 2.
15 * - 1 -> 2 -> 1 -> 2.
16 * - 1 -> 0 -> 1 -> 2.
17 * It can be proven that no other way is possible, so we return 3.
18 * Example 2:
19 *
20 * Input: startPos = 2, endPos = 5, k = 10
21 * Output: 0
22 * Explanation: It is impossible to reach position 5 from position 2 in exactly 10 steps.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= startPos, endPos, k <= 1000
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/
33// discuss: https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn number_of_ways(start_pos: i32, end_pos: i32, k: i32) -> i32 {
39 0
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_2400() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.