java.lang.Object
g2301_2400.s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps.Solution

public class Solution extends Object
2400 - Number of Ways to Reach a Position After Exactly k Steps.<p>Medium</p> <p>You are given two <strong>positive</strong> integers <code>startPos</code> and <code>endPos</code>. Initially, you are standing at position <code>startPos</code> on an <strong>infinite</strong> number line. With one step, you can move either one position to the left, or one position to the right.</p> <p>Given a positive integer <code>k</code>, return <em>the number of <strong>different</strong> ways to reach the position</em> <code>endPos</code> <em>starting from</em> <code>startPos</code><em>, such that you perform <strong>exactly</strong></em> <code>k</code> <em>steps</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>Two ways are considered different if the order of the steps made is not exactly the same.</p> <p><strong>Note</strong> that the number line includes negative integers.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> startPos = 1, endPos = 2, k = 3</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> We can reach position 2 from 1 in exactly 3 steps in three ways:</p> <ul> <li> <p>1 -> 2 -> 3 -> 2.</p> </li> <li> <p>1 -> 2 -> 1 -> 2.</p> </li> <li> <p>1 -> 0 -> 1 -> 2.</p> </li> </ul> <p>It can be proven that no other way is possible, so we return 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> startPos = 2, endPos = 5, k = 10</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> It is impossible to reach position 5 from position 2 in exactly 10 steps.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= startPos, endPos, k <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfWays

      public int numberOfWays(int startPos, int endPos, int k)