Conversation
There was a problem hiding this comment.
Pull request overview
Adds/updates multi-language solutions and documentation for LeetCode 2751 (Robot Collisions), standardizing implementations around a sorted-index + stack collision simulation.
Changes:
- Refactors TS/JS/Java/Go/C++ solutions to a consistent “sorted indices + stack” collision loop and returns survivors with
health > 0. - Simplifies Python implementation and aligns variable naming (
idx,stk,ans) across languages. - Expands README (EN/ZH) with an explicit “Stack Simulation” approach description and updated code snippets.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| solution/2700-2799/2751.Robot Collisions/Solution.ts | Refactors to sorted indices + stack simulation; simplifies survivor filtering. |
| solution/2700-2799/2751.Robot Collisions/Solution.py | Simplifies implementation and returns survivors via filtering; removes type hints. |
| solution/2700-2799/2751.Robot Collisions/Solution.js | Mirrors TS refactor with stack simulation and health > 0 filter. |
| solution/2700-2799/2751.Robot Collisions/Solution.java | Refactors to Deque-based stack; simplifies indexing/sorting and final collection. |
| solution/2700-2799/2751.Robot Collisions/Solution.go | Refactors to sorted indices + stack; renames locals and returns ans. |
| solution/2700-2799/2751.Robot Collisions/Solution.cpp | Refactors to sorted indices + vector-as-stack; returns survivors. |
| solution/2700-2799/2751.Robot Collisions/README_EN.md | Adds approach explanation; updates code snippets to match refactor. |
| solution/2700-2799/2751.Robot Collisions/README.md | Adds Chinese approach explanation; updates code snippets to match refactor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| Integer[] idx = new Integer[n]; | ||
| Arrays.setAll(idx, i -> i); | ||
| Arrays.sort(idx, (a, b) -> positions[a] - positions[b]); |
There was a problem hiding this comment.
The comparator positions[a] - positions[b] can overflow for large positions values, producing an incorrect sort order and therefore incorrect collision outcomes. Use Integer.compare(positions[a], positions[b]) (or Comparator.comparingInt(i -> positions[i])) to avoid overflow.
| Arrays.sort(idx, (a, b) -> positions[a] - positions[b]); | |
| Arrays.sort(idx, (a, b) -> Integer.compare(positions[a], positions[b])); |
| } | ||
| Integer[] idx = new Integer[n]; | ||
| Arrays.setAll(idx, i -> i); | ||
| Arrays.sort(idx, (a, b) -> positions[a] - positions[b]); |
There was a problem hiding this comment.
The Java snippet uses positions[a] - positions[b] as a comparator, which can overflow and is not a safe example to document. Update the README snippet to use Integer.compare(positions[a], positions[b]) (or Comparator.comparingInt(...)) so the documentation matches a correct, overflow-safe implementation.
| Arrays.sort(idx, (a, b) -> positions[a] - positions[b]); | |
| Arrays.sort(idx, (a, b) -> Integer.compare(positions[a], positions[b])); |
| n = len(positions) | ||
| indices = list(range(n)) | ||
| stack = [] | ||
| def survivedRobotsHealths(self, positions, healths, directions): |
There was a problem hiding this comment.
This change removes the previously-present type hints (List[int], str, return type). If this repository is keeping typed Python solutions (as the previous version suggests), consider restoring the annotations (and the relevant typing import) to improve readability and enable static checking.
No description provided.