Similar Problems

Similar Problems not available

Count Collisions Of Monkeys On A Polygon - Leetcode Solution

Companies:

LeetCode:  Count Collisions Of Monkeys On A Polygon Leetcode Solution

Difficulty: Medium

Topics: math  

The problem "Count Collisions Of Monkeys On A Polygon" on LeetCode asks us to find the number of collisions that occur among a group of monkeys that move along a polygon. The polygon is represented by an array of points, while the positions of the monkeys are given by an array of their distances along the polygon from a reference point.

To solve this problem, we need to simulate the movement of the monkeys along the polygon. We can do this by using two pointers, one to keep track of the current position of each monkey and another to keep track of the next position on the polygon. We can then check if any of the monkeys collide with each other at each step of the simulation.

Let's look at the detailed solution step by step:

Step 1: Define the variables and initialize them

Before starting the simulation, we need to define some variables and initialize them.

  • count: To keep track of the number of collisions between monkeys.
  • n: To store the number of points in the polygon.
  • m: To store the number of monkeys.
  • p: To store the array of points.
  • a: To store the array of distances of monkeys from the reference point.

Here is how we can define and initialize these variables:

int count = 0, n = p.size(), m = a.size();

Step 2: Start the simulation

Now, we can start the simulation by iterating over the array of points and checking if any of the monkeys collide at each step of the simulation.

for (int i = 0; i < n; i++) { // iterate over the polygon points
   int j = (i+1)%n; // next point on the polygon
   for(int k = 0; k < m; k++){ // iterate over the monkeys
       int curr = a[k]%n; // current position of the monkey
       int next = (curr + 1)%n; // next position of the monkey
       if(a[k] == i || a[k] == j) continue; // monkey exactly at the point do not collide with each other
       if(i == curr || i == next || j == curr || j == next) count++; // when a monkey is at either corner of a segment it collides.
       if((i<curr&&j<curr&&a[k]<curr) || (i>curr&&j>curr&&a[k]>curr)) continue; // monkeys in the same semi-infinite line do not collide
       if((i>j) != (curr>next) && (i-j)*(curr-next)>(curr-j)*(i-next)) count++; // compute the intersection point to determine collision
   }
}

Step 3: Check for collisions

We can determine if there is a collision between two monkeys by checking if they are on the same segment of the polygon and if they are moving towards each other. To do this, we can compute the intersection point of the segment and the line connecting the current and next positions of each monkey.

Here is how we can implement this:

double x1 = p[i][0], y1 = p[i][1];
double x2 = p[j][0], y2 = p[j][1];
double x3 = p[curr][0], y3 = p[curr][1];
double x4 = p[next][0], y4 = p[next][1];

double denom = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1);
if (denom == 0) continue; // segment and line are parallel
double ua = ((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))/denom;
double ub = ((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))/denom;
if(ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) count++; // segment and line intersect, so collision happens

Step 4: Return the result

After finishing the simulation, we can return the number of collisions that occurred among the monkeys.

Here is the complete implementation of the function:

int countCollisions(vector<vector<int>>& p, vector<int>& a) {
    int count = 0, n = p.size(), m = a.size();
    for (int i = 0; i < n; i++) { // iterate over the polygon points
       int j = (i+1)%n; // next point on the polygon
       for(int k = 0; k < m; k++){ // iterate over the monkeys
           int curr = a[k]%n; // current position of the monkey
           int next = (curr + 1)%n; // next position of the monkey
           if(a[k] == i || a[k] == j) continue; // monkey exactly at the point do not collide with each other
           if(i == curr || i == next || j == curr || j == next) count++; // when a monkey is at either corner of a segment it collides.
           if((i<curr&&j<curr&&a[k]<curr) || (i>curr&&j>curr&&a[k]>curr)) continue; // monkeys in the same semi-infinite line do not collide
           double x1 = p[i][0], y1 = p[i][1];
           double x2 = p[j][0], y2 = p[j][1];
           double x3 = p[curr][0], y3 = p[curr][1];
           double x4 = p[next][0], y4 = p[next][1];

           double denom = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1);
           if (denom == 0) continue; // segment and line are parallel
           double ua = ((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))/denom;
           double ub = ((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))/denom;
           if(ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) count++; // segment and line intersect, so collision happens
       }
    }
    return count/2;
}

Note that we divide the count by 2 at the end because each collision is counted twice, once for each monkey involved in the collision.

This implementation has a time complexity of O(nm), where n is the number of points in the polygon and m is the number of monkeys. It also has a space complexity of O(1), as we are using only a constant amount of extra space.

Count Collisions Of Monkeys On A Polygon Solution Code

1