Skip to content

N robots collisions Solution#7440

Open
NilayBadjatya wants to merge 2 commits into
TheAlgorithms:masterfrom
NilayBadjatya:N-Robots-Collisions
Open

N robots collisions Solution#7440
NilayBadjatya wants to merge 2 commits into
TheAlgorithms:masterfrom
NilayBadjatya:N-Robots-Collisions

Conversation

@NilayBadjatya
Copy link
Copy Markdown

  • [ x] I have read CONTRIBUTING.md.
  • [ x] This pull request is all my own work -- I have not plagiarized it.
  • [ x] All filenames are in PascalCase.
  • [ x] All functions and variable names follow Java naming conventions.
  • [ x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • [ x] All new algorithms include a corresponding test class that validates their functionality.
  • [ x] All new code is formatted with clang-format -i --style=file path/to/your/file.java

Copy link
Copy Markdown
Contributor

@prashantpiyush1111 prashantpiyush1111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test file found. Please add NRobotsCollisionTest.java as required by contributing guidelines. Also add a Wikipedia or reference URL in the class Javadoc.

@@ -0,0 +1,54 @@
package com.thealgorithms.simulation;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing imports: java.util.ArrayList and java.util.List need to be added here. This is causing the build failure.

* the direction of the corresponding robot
* @return the earliest collision time, or -1 if no collision occurs
*/
public static int earliestCollisionTime(int n, int[] positions, String directions) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type should be double instead of int. Collision time can be fractional — e.g., if distance between two robots is 3, time = 1.5 but int division will incorrectly return 1.

*/
public static int earliestCollisionTime(int n, int[] positions, String directions) {
List<Pair> sortedDirections = new ArrayList<>();
for(int i = 0; i < n; i++){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang format issue: Please add space after forfor (int i = 0; i < n; i++) {

}
sortedDirections.sort((a, b) -> Integer.compare(a.first, b.first));
int minTime = Integer.MAX_VALUE;
for(int i = 1; i < n; i++){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same clang format issue here as well.

minTime = Math.min(minTime, sortedDirections.get(i).first - sortedDirections.get(i - 1).first);
}
}
return minTime == Integer.MAX_VALUE ? -1 : minTime / 2;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: minTime / 2 does integer division. Change minTime to double or cast: (double) minTime / 2 to handle fractional collision times correctly.

@prashantpiyush1111
Copy link
Copy Markdown
Contributor

Hi @NilayBadjatya! The build is failing due to these issues:

1. Missing imports (Build failure)
Please add at the top of the file:
import java.util.ArrayList;
import java.util.List;

2. Clang format issues
Please add spaces:

  • for(for (
  • if(if (

3. Missing test file
Please add NRobotsCollisionTest.java as required by contributing guidelines.

4. Return type bug
int division will give wrong answer for odd distances. Please change return type to double and return (double) minTime / 2.

Fix these and all CI checks should pass!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants