LeetCode题解(0874):模拟行走的机器人(Python)

题目:原题链接(简单)

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N × K ) O(N×K) O(N×K) O ( 1 ) O(1) O(1)超出实际限制
Ans 2 (Python) O ( N + K ) O(N+K) O(N+K) O ( K ) O(K) O(K)468ms (68.22%)
Ans 3 (Python) O ( N + K ) O(N+K) O(N+K) O ( K ) O(K) O(K)472ms (64.68%)

LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。

解法一(模拟情景):

def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
    orient = (0, 1)  # 机器人的方向: 北=[0,1];东=[1,0];南=[0,-1];西=[-1,0]
    now = (0, 0)

    ans = 0
    for command in commands:
        if command == -2:
            if orient == (0, 1):
                orient = (-1, 0)
            elif orient == (-1, 0):
                orient = (0, -1)
            elif orient == (0, -1):
                orient = (1, 0)
            else:
                orient = (0, 1)
        elif command == -1:
            if orient == (0, 1):
                orient = (1, 0)
            elif orient == (1, 0):
                orient = (0, -1)
            elif orient == (0, -1):
                orient = (-1, 0)
            else:
                orient = (0, 1)

        for _ in range(command):
            aim = [now[0] + orient[0], now[1] + orient[1]]
            if aim not in obstacles:
                now = aim
                ans = max(ans, now[0] ** 2 + now[1] ** 2)
    return ans

解法二(将obstacles转换为set):

def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
    orient = (0, 1)  # 机器人的方向: 北=[0,1];东=[1,0];南=[0,-1];西=[-1,0]

    obstacles = set(map(tuple, obstacles))

    x = 0
    y = 0
    ans = 0

    for command in commands:
        if command == -2:
            if orient == (0, 1):
                orient = (-1, 0)
            elif orient == (-1, 0):
                orient = (0, -1)
            elif orient == (0, -1):
                orient = (1, 0)
            else:
                orient = (0, 1)
        elif command == -1:
            if orient == (0, 1):
                orient = (1, 0)
            elif orient == (1, 0):
                orient = (0, -1)
            elif orient == (0, -1):
                orient = (-1, 0)
            else:
                orient = (0, 1)

        for _ in range(command):
            aim = (x + orient[0], y + orient[1])
            if aim not in obstacles:
                x += orient[0]
                y += orient[1]
                ans = max(ans, x * x + y * y)

    return ans

解法三(优雅的转弯):

def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
    obstacles = set(map(tuple, obstacles))

    ans = 0

    x = 0
    y = 0
    dx = 0  # 机器人方向
    dy = 1  # 机器人方向

    for command in commands:
        if command == -2:
            dx, dy = (-dy, dx)
        elif command == -1:
            dx, dy = (dy, -dx)

        for _ in range(command):
            aim = (x + dx, y + dy)
            if aim not in obstacles:
                x += dx
                y += dy
                ans = max(ans, x * x + y * y)

    return ans
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值