LeetCode-Feedback / LeetCode-Feedback

671 stars 330 forks source link

Existing test cases - 874. Walking Robot Simulation #14875

Closed fatalerror-i closed 1 year ago

fatalerror-i commented 1 year ago

Category of the bug

Description of the bug

In the description the robot starts at point (0, 0) and cannot reach any of the obscales, however (0, 0) appears in test cases leading to unexpected result. Solution using hashmap does not run into this problem.

LC-Pam commented 1 year ago

Hi there,

Thank you for reaching out to us.


We've relayed this issue to our team to investigate.


We will reach back to you after we have the results.

Thank you for your support.

fatalerror-i commented 1 year ago

(continue from above)

Code

class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        hor = defaultdict(list)
        ver = defaultdict(list)
        for x, y in obstacles:
            hor[y].append(x)
            ver[x].append(y)
        for x in ver:
            ver[x].sort()
        for y in hor:
            hor[y].sort()

        x, y = 0, 0
        k = 0
        ans = 0
        for cmd in commands:
            if cmd == -2:
                k = (k - 1) % 4
            elif cmd == -1:
                k = (k + 1) % 4
            else:
                d = 1 - k // 2 * 2
                if k % 2 == 1:
                    x2 = x + d * cmd
                    if d == 1:
                        idx = bisect_left(hor[y], x)
                        if idx < len(hor[y]) and x2 >= hor[y][idx]:
                            x2 = hor[y][idx] - 1
                    else:
                        idx = bisect_right(hor[y], x) - 1
                        if idx >= 0 and x2 <= hor[y][idx]:
                            x2 = hor[y][idx] + 1
                    x = x2
                else:
                    y2 = y + d * cmd
                    if d == 1:
                        idx = bisect_left(ver[x], y)
                        if idx < len(ver[x]) and y2 >= ver[x][idx]:
                            y2 = ver[x][idx] - 1
                    else:
                        idx = bisect_right(ver[x], y) - 1
                        if idx >= 0 and y2 <= ver[x][idx]:
                            y2 = ver[x][idx] + 1
                    y = y2
                ans = max(ans, x * x + y * y)
        return ans

Language

Python3

Test cases

Input:

[1,1,1]
[[0,0]]

Output:

1

Expected output:

9

The solution with bisect is logically correct, and passes test cases without [0,0] in obscales, while fails in test case with [0,0] in obscales, which is unexpected. I have to change bisect_left(hor[y], x) to bisect_left(hor[y], x + 1), and bisect_right(hor[y], x) to bisect_right(hor[y], x - 1) with respect to horizontal moves, to pass all test cases.

LC-Pam commented 1 year ago

Thank you for your time.

We've used your feedback to edit the Problem description.

Your LeetCode account will receive 100 LeetCoins as a reward for this feedback.

Please reply to us with your LCUS username.

If you have any other questions or feedback, please don't hesitate to let us know!

We appreciate your support!

fatalerror-i commented 1 year ago

My most frequently used account is LCCN account FatalError. And I'd appreciate it if the description and test cases on LCCN would be updated.

Thanks!