java.lang.Object
g2501_2600.s2550_count_collisions_of_monkeys_on_a_polygon.Solution

public class Solution extends Object
2550 - Count Collisions of Monkeys on a Polygon.<p>Medium</p> <p>There is a regular convex polygon with <code>n</code> vertices. The vertices are labeled from <code>0</code> to <code>n - 1</code> in a clockwise direction, and each vertex has <strong>exactly one monkey</strong>. The following figure shows a convex polygon of <code>6</code> vertices.</p> <p><img src="https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg" alt="" /></p> <p>Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex <code>i</code> can be:</p> <ul> <li>the vertex <code>(i + 1) % n</code> in the clockwise direction, or</li> <li>the vertex <code>(i - 1 + n) % n</code> in the counter-clockwise direction.</li> </ul> <p>A <strong>collision</strong> happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge.</p> <p>Return <em>the number of ways the monkeys can move so that at least <strong>one collision</strong></em> <em>happens</em>. Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that each monkey can only move once.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 3</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> There are 8 total possible movements.</p> <p>Two ways such that they collide at some point are:</p> <ul> <li>Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.</li> <li>Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide. It can be shown 6 total movements result in a collision.</li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> 14</p> <p><strong>Explanation:</strong> It can be shown that there are 14 ways for the monkeys to collide.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= n <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • monkeyMove

      public int monkeyMove(int n)