zoukankan      html  css  js  c++  java
  • 874.Walking Robot Simulation(list不可被哈希)

    A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands:

    • -2: turn left 90 degrees
    • -1: turn right 90 degrees
    • 1 <= x <= 9: move forward x units

    Some of the grid squares are obstacles.

    The i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1])

    If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)

    Return the square of the maximum Euclidean distance that the robot will be from the origin.

    Example 1:

    Input: commands = [4,-1,3], obstacles = []
    Output: 25
    Explanation: robot will go to (3, 4)

    Example 2:

    Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
    Output: 65
    Explanation: robot will be stuck at (1, 4) before turning left and going to (1, 8)

    Note:

    • 0 <= commands.length <= 10000
    • 0 <= obstacles.length <= 10000
    • -30000 <= obstacle[i][0] <= 30000
    • -30000 <= obstacle[i][1] <= 30000
    • The answer is guaranteed to be less than 2 ^ 31.

    Solution1:(TLE)

    class Solution:
        def robotSim(self, commands, obstacles):
            """
            :type commands: List[int]
            :type obstacles: List[List[int]]
            :rtype: int
            """
            pos = [0,0]
            direction = 0
            def turn(pre,new):
                if new == -1:
                    now = (pre + 1)%4
                else:
                    now = (pre - 1)%4
                return now
            def move(direction,distance):
                if direction==0:
                    while distance>0:
                        distance -= 1
                        pos[1] += 1
                        if pos in obstacles:
                            pos[1] -= 1
                            break
                if direction==1:
                    while distance>0:
                        distance -= 1
                        pos[0] += 1
                        if pos in obstacles:
                            pos[0] -= 1
                            break
                if direction==2:
                    while distance>0:
                        distance -= 1
                        pos[1] -= 1
                        if pos in obstacles:
                            pos[1] += 1
                            break
                if direction==3:
                    while distance>0:
                        distance -= 1
                        pos[0] -= 1
                        if pos in obstacles:
                            pos[0] += 1
                            break
                return
            res = 0
            for i in commands:
                # print(i)
                if i>0:
                    move(direction,i)
                    # print('pos:',pos,"direction:",direction)
                else:
                    direction = turn(direction,i)
                    # print('pos:',pos,"direction:",direction)
                res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
            return res
    

    有一个结果过不了,百思不得其解,画了图对照走了一遍也没有错。最后发现返回的结果是中间任意时刻离原点的最大值,而不是最终的结果,太坑了。

    Solution2:

    class Solution:
        def robotSim(self, commands, obstacles):
            """
            :type commands: List[int]
            :type obstacles: List[List[int]]
            :rtype: int
            """
            s = set()
            for i in obstacles:
                s.add(tuple(i))
            pos = [0,0]
            direction = 0
            def turn(pre,new):
                if new == -1:
                    now = (pre + 1)%4
                else:
                    now = (pre - 1)%4
                return now
            def move(direction,distance):
                if direction==0:
                    while distance>0:
                        distance -= 1
                        pos[1] += 1
                        if tuple(pos) in s:
                            pos[1] -= 1
                            break
                if direction==1:
                    while distance>0:
                        distance -= 1
                        pos[0] += 1
                        if tuple(pos) in s:
                            pos[0] -= 1
                            break
                if direction==2:
                    while distance>0:
                        distance -= 1
                        pos[1] -= 1
                        if tuple(pos) in s:
                            pos[1] += 1
                            break
                if direction==3:
                    while distance>0:
                        distance -= 1
                        pos[0] -= 1
                        if tuple(pos) in s:
                            pos[0] += 1
                            break
                return
            res = 0
            for i in commands:
                # print(i)
                if i>0:
                    move(direction,i)
                    # print('pos:',pos,"direction:",direction)
                else:
                    direction = turn(direction,i)
                    # print('pos:',pos,"direction:",direction)
                res = max(pos[0] * pos[0] + pos[1] * pos[1], res)
            return res
    

    改用set存储obstacle而不是list,不再超时了。set查找元素是哈希的方法,需要O(1),而list则是O(n)。
    注意list是不可以被哈希的,故改用tuple来存储到set中。

  • 相关阅读:
    重置SQLSERVER表的自增列,让自增列重新计数
    【PLM】【PDM】60页PPT终于说清了PDM和PLM的区别;智造时代,PLM系统10大应用趋势!
    数据库设计规范
    不要听吹牛逼什么前端MVVM框架就是好,其实都是一帮没学好分层设计的搞出来的,让你彻底看清前端MVVM的本质
    SQL数据库日志清理
    1.3、安装MySQL、MySQLdb、Pycharm和其他软件
    1.2.2.3、Django虚拟目录作用以及文件说明
    nginx设置图片防盗链和白名单
    php-5.5.7升级,编译配置方法
    mysql函数CONCAT_WS()比CONCAT()更方便
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9751086.html
Copyright © 2011-2022 走看看