Skip to content

Commit 7514376

Browse files
Merge pull request #25 from paxtonfitzpatrick/main
added some "refining the problem" notes about short-circuit cases
2 parents ec4d61d + 109be95 commit 7514376

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Diff for: problems/2751/paxtonfitzpatrick.md

+6
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@
5757

5858
## Refining the problem, round 2 thoughts
5959

60+
- I'll add a "short-circuit" condition where we check whether all robots are moving in the same direction, and if so, return `healths` immediately
61+
- There's another possible circumstance where we could short-circuit early -- if all robots moving leftward start out to the left of all robots that're moving rightward. But I'm not sure this is really worth implementing because we'd need to check this against a sorted `position` list, which would require at least an additional loop to run for all test cases, and would probably end up taking more time overall since I'm willing to bet this scenario appears once, if at all. So I'll forego this.
62+
6063
## Attempted solution(s)
6164

6265
```python
6366
class Solution:
6467
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
68+
if all(d == directions[0] for d in directions):
69+
return healths
70+
6571
sorted_ixs = list(range(len(positions)))
6672
sorted_ixs.sort(key=lambda i: positions[i])
6773
right_movers_stack = []

0 commit comments

Comments
 (0)