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中。

  • 相关阅读:
    bzoj千题计划239:bzoj4069: [Apio2015]巴厘岛的雕塑
    bzoj千题计划238:bzoj3668: [Noi2014]起床困难综合症
    hdu 3022 Sum of Digits
    在RAC中,当私有网线拔了后,会怎么样?
    CVU介绍
    oracle rac 日志体系结构!
    oracle 内存结构 share pool sql解析的过程
    Oracle逻辑读详解
    共享池之八:软解析、硬解析、软软解析 详解一条SQL在library cache中解析涉及的锁
    共享池之六:shared pool latch/ library cache latch /lock pin 简介
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9751086.html
Copyright © 2011-2022 走看看