js- brush prove safety record title (stacks and queues)

1. The two stacks queue

Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.

My solution

Stack LIFO, FIFO queue, enqueue operation nothing to say, directly into the instack on the line. Instack dequeue the data inside a pop from the stack, and then a outstack received, so that the first pushed data instack went outstack of the stack, the stack is popped return once a pop operation.
Note also are two stacks of empty cases, good judgment.

let instack = []
let outstack = []
function push(node)
{
    instack.push(node)
}
function pop()
{
    if(outstack.length == 0){
        while(instack.length !=0){
            outstack.push(instack.pop())
        }
    }
    return outstack.pop()
}

Supplement

So the question is? How to queue stack imitate it?
Stack push operation or a complete, but again the stack requires a queue. Data stored in the queue 1shfit queue 2, queue 1 only when a data, shift out, and then queue data to be moved back to the queue 2 1

function stackpush(num){
    inqueue.push(num)
}
function stackpop(queue){
    let res = null
    if(queue.length != 0){
        while(queue.length !=1){
            outqueue.push(queue.shift())
        }
        res = queue.shift()
        while(outqueue.length != 0){
            queue.push(outqueue.shift())
        }
    }
    return res
}
let inqueue=[]
let outqueue =[]
stackpush(1)
stackpush(2)
stackpush(3)
stackpush(4)
console.log(inqueue); //[1,2,3,4]
console.log(stackpop(inqueue)); //4
console.log(stackpop(inqueue)); //3

2. The sliding window maximum

Given an array and the size of the sliding window, sliding window to find the maximum value among all values. For example, if the input array size and {2,3,4,2,6,2,5,1} 3 sliding window, then the presence of a total of six sliding window, their maximum values ​​is {4,4,6, 6,6,5}; it has the following six array {2,3,4,2,6,2,5,1} for the sliding window: {[2,3,4], 2,6,2,5 , 1}, {2, [3,4,2], 6,2,5,1}, {2,3, [4,2,6], 2, 5}, {2,3,4 , [2,6,2], 5,1}, {2,3,4,2, [6,2,5], 1}, {2,3,4,2,6, [2,5, 1]}.

My solution

Of course, a stupid comparison, two for loop, a number of size taken out into the empty array arr and comparing the size of the maximum value stored in the output res

function maxInWindows(num, size)
{
    let res = []
    if(size == 0 ||num == null){
        return res
    }
    for(let i=0;i<=num.length-size;i++){
        let arr = []
        for(let j =i;j<i+size;j++){
            arr.push(num[j])
        }
        arr.sort((a,b)=>{
            return a-b
        })
        res.push(arr[arr.length-1])
    }
    return res
}

Other analysis

Deque: two can enqueue and dequeue, js is the push and pop, shift and unshift. Deque save the current window, the largest index number, the new head deque always current window of the maximum number.
With this index, we can know whether the new window also contains the largest number of the original, if not contained, we put the old number is deleted from the first double-ended queue.

function maxInWindows(num, size)
{
    let res = []
    if(size == 0){
        return res
    }
    
    let queue = []
    for(let i=0;i<num.length;i++){
        let begin = i-size+1  //窗起始位置
        //新输入的num数据比queue标识的进入滑动窗的数据大
        //就把小的数弹出不要,保证queue头部标示的数是滑动窗口中最大的
        while((queue.length !=0)&&(num[queue[queue.length-1]]<=num[i])){
            queue.pop()   
        }                 
        
        //判断窗有没有移到最大值位置的后面,也就是说当前最大值已经不在窗内了,需要弹出
        if(queue.length !=0&&begin>queue[0]){
            queue.shift()
        } 
        queue.push(i)
        if(begin>=0){
            res.push(num[queue[0]])
        }
    }
    return res
}

https://leetcode-cn.com/problems/sliding-window-maximum/solution/shi-pin-jie-xi-shuang-duan-dui-lie-hua-dong-chuang/
video buckle force given details the role of the queue

3. Function min stack comprising

Stack data structure definition, implement this type can be a min function smallest elements contained in the stack (should the time complexity O (1)).
Note: to ensure that the test is not when the stack is empty, calls on the stack pop () or min () or top () method.

My solution

For the min function, the idea is to start traversing the stack, the extent of the current record variable minimum value, the iteration is complete return variable. However, there is a requirement title time complexity is O (1). Traversal time complexity is O (n) and the minimum value is popped off the stack, once, again need to traverse.

Other analysis

By establishing the auxiliary stack min () function is reduced complexity of O (1):

  • Data Stack A: A stack is used to store all the elements to ensure that push (), pop () function, Top () normal logic function.
  • Auxiliary Stack B: A stack each time the press-fitting, the minimum value of which is pressed into the stack A stack which B, A and stack corresponding to the smallest element is always the top element of the stack B, i.e., min () function simply returns the stack top element to B, respectively.

Four functions:

  • push (node): B to ensure non-strictly decreasing elements, an element A corresponds to an element of
    • A node is pressed into the stack
    • B stack is empty, node pushed onto the stack; if not empty, node B is smaller than the top of the stack, the stack pressure
  • pop (): A top element and the top element B are the same, that is to say exactly the minimum stack A, B pop the top element to be synchronized
  • top():A.pop()
  • min (): Returns the top element B
let A = []
let B = []
function push(node)
{
    A.push(node)
    //注意这里一定包含等于的情况,如果A栈压入3,2,4,5,2,B栈就是3,2,2
    //如果不包含等于,那么B栈就是3,2;这是A栈弹出2,它的最小值还是2,但是B栈以及不包含2了,会出错
    if(B.length == 0||node<=B[B.length-1])  
        B.push(node)
}
function pop()
{
    //B栈弹出是有条件的,如果A栈顶刚好是最小值,那么B栈顶肯定也是这个值,如果A弹出,B不弹出,A的最小值就会出错
    //B栈也不能随便跟着A栈弹
    if(A[A.length-1]==B[B.length-1]) {B.pop()}
    A.pop()
}
function top()
{
    return A[A.length-1]
}
function min()
{
    return B[B.length-1]
}

4. The stack push, pop sequence

Two input sequence of integers, the first sequence representing a pressed stack order, determines whether it is possible for the second sequence the order of the pop-up stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)

My solution

Pressed traversal sequence elements are pushed onto the stack, and pop up the same as an element sequence, elements of the stack pop stack, pop-up-shift the shift sequence, comparison continues.
But the time to continue the analysis, found that relatively pressed into the sequence elements and pop-up sequences are equal decide whether stack, the stack when needed, the code does not write (since I did not write it.)

Other analysis

I think that is a key, pop-up sequence is a collection of former top element. It should be a relatively top of the stack and pop-up sequence to determine whether the element is pushed onto the stack sequence: inequality has been pushed onto the stack, the stack when it is equal, a pop-up sequence shift.

function IsPopOrder(pushV, popV)
{
    if(pushV == null||popV == null) return false
    let stack = []
    let j = 0
    for(let i=0;i<pushV.length;i++){ //其实一直在入栈,然后再判断栈顶元素
         stack.push(pushV[i])
         //这里一定是while 
         while(stack.length!=0&&stack[stack.length-1]==popV[j]){
            stack.pop()
            j++
        }
    }
    //入栈序列遍历完了,如果弹出序列匹配应该stack一边入栈一边出栈,最后stack应该是空的
    if(stack.length == 0) 
        return true
    return false
}
Published 21 original articles · won praise 0 · Views 181

Guess you like

Origin blog.csdn.net/adrenalineiszzz/article/details/104804250