isaacpei / algorithm-interview

Weekly algorithm questions for interview
18 stars 0 forks source link

Q002_大菠萝_solution #14

Open biganans opened 6 years ago

biganans commented 6 years ago

Question_ID: Q{002}

Language: Lua

Time Cost: 35-mins

Time Complexity: {}

Solution

  1. 判定读取到的输入是否合法
  2. get_max查找最大的数
  3. 输出

My Code

print("Please input \n1:push x(number)\n2:pop \n3:get_max")
-- 保存数据的table
local stack = {}
local maxStack = {}
local getMax = function()
    local num =  maxStack[1]
    return num == nil and -1 or num
end

local setNumber = function(num)
    if #maxStack == 0 then 
        maxStack[1] = num 
    else
        for i=1,#maxStack do
            if maxStack[i] < num then
                table.insert(maxStack,i,num)
                return
            elseif i == #maxStack then
                table.insert(maxStack,i+1,num)
            end
        end
    end
end
-- 检查语法是否正确
local check = function(str)
    local s = string.sub(str,1,4)
    if s == "push" then
        local num = tonumber(string.sub(str,6))
        if num then
            print("None")
            stack[#stack+1] = num
            setNumber(num)
            return
        end
    else
        if #str == #"pop" and str == "pop" then
            if #stack > 0 then
                local num = stack[1]
                print(num)
                table.remove(stack,1)
                for i=1,#maxStack do
                    if maxStack[i] == num then
                        table.remove(maxStack,i)
                        break
                    end
                end
            else
                print("-1")
            end
            return
        else
            if #str == #"get_max" and str == "get_max" then
                if #stack > 0 then
                    print(getMax())
                else
                    print("-1")
                end
                return
            end
        end
    end
    print("Error input ,please input \n1:push x(number)\n2:pop \n3:get_max")
end

while(1) do
    local readStr = io.read()
    local p,n = check(readStr)
    if p then
        print(p)
    end
end

Other

Lua table like stack

rbee3u commented 6 years ago

你目前的这个实现其它都没问题,get_max效率比较低下,这题的本意应该就是你要使用点儿黑科技让 get_max 比较快。

biganans commented 6 years ago

修改了一发

print("Please input \n1:push x(number)\n2:pop \n3:get_max")
-- 保存数据的table
local stack = {}
local maxStack = {}
local getMax = function()
    local num =  maxStack[1]
    return num == nil and -1 or num
end

local setNumber = function(num)
    if #maxStack == 0 then 
        maxStack[1] = num 
    else
        -- for i=1,#maxStack do
        --  if maxStack[i] < num then
        --      table.insert(maxStack,i,num)
        --      return
        --  elseif i == #maxStack then
        --      table.insert(maxStack,i+1,num)
        --  end
        -- end
        -- 参照m75n修改
        while #maxStack > 0 and maxStack[#maxStack] < num do
            table.remove(maxStack,#maxStack)
        end
        maxStack[1+#maxStack] = num
    end
end
-- 检查语法是否正确
local check = function(str)
    local s = string.sub(str,1,4)
    if s == "push" then
        local num = tonumber(string.sub(str,6))
        if num then
            print("None")
            stack[#stack+1] = num
            setNumber(num)
            return
        end
    else
        if #str == #"pop" and str == "pop" then
            if #stack > 0 then
                local num = stack[1]
                print(num)
                table.remove(stack,1)
                -- for i=1,#maxStack do
                --  if maxStack[i] == num then
                --      table.remove(maxStack,i)
                --      break
                --  end
                -- end
                -- 参照m75n修改
                if maxStack[1] == num then
                    table.remove(maxStack,1)
                end
            else
                print("-1")
            end
            return
        else
            if #str == #"get_max" and str == "get_max" then
                if #stack > 0 then
                    print(getMax())
                else
                    print("-1")
                end
                return
            end
        end
    end
    print("Error input ,please input \n1:push x(number)\n2:pop \n3:get_max")
end

while(1) do
    local readStr = io.read()
    local p,n = check(readStr)
    if p then
        print(p)
    end
end
isaacpei commented 6 years ago

眼睛疼...看不下去... 建议不要写看起来太难受的代码