java.lang.Object
g1801_1900.s1866_number_of_ways_to_rearrange_sticks_with_k_sticks_visible.Solution

public class Solution extends Object
1866 - Number of Ways to Rearrange Sticks With K Sticks Visible.<p>Hard</p> <p>There are <code>n</code> uniquely-sized sticks whose lengths are integers from <code>1</code> to <code>n</code>. You want to arrange the sticks such that <strong>exactly</strong> <code>k</code> sticks are <strong>visible</strong> from the left. A stick is <strong>visible</strong> from the left if there are no <strong>longer</strong> sticks to the <strong>left</strong> of it.</p> <ul> <li>For example, if the sticks are arranged <code>[1,3,2,5,4]</code>, then the sticks with lengths <code>1</code>, <code>3</code>, and <code>5</code> are visible from the left.</li> </ul> <p>Given <code>n</code> and <code>k</code>, return <em>the <strong>number</strong> of such arrangements</em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 3, k = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> [1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible. The visible sticks are underlined.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 5, k = 5</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> [1,2,3,4,5] is the only arrangement such that all 5 sticks are visible. The visible sticks are underlined.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 20, k = 11</p> <p><strong>Output:</strong> 647427950</p> <p><strong>Explanation:</strong> There are 647427950 (mod 10<sup>9</sup> + 7) ways to rearrange the sticks such that exactly 11 sticks are visible.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 1000</code></li> <li><code>1 <= k <= n</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rearrangeSticks

      public int rearrangeSticks(int n, int k)