面试算法题:基于给定数组A与栈B构造栈C(C#)
Hey there! Let's break down this interview algorithm problem and work through a solution that fits your requirements of O(N²) time complexity.
First, let's make sure we're on the same page with the problem statement (I'll clarify based on the details you provided):
Given an array
A(size N, distinct integers 1~N) and a stackB(size N, distinct integers 1~N):
- For each index
iinA, the valueA[i]tells us the position (counting from the top ofB, where top = position 1) of the element that should be placed at positioni(counting from the top ofC, top = position 0 or 1—we'll use 1-based for clarity) in stackC.- Our goal is to construct stack
Cfollowing these rules, with an O(N²) time algorithm.
Since stacks only let us access the top element, we can't directly grab an element from the middle of B. To get an element that's k positions from the top, we'll need to pop the top k-1 elements, retrieve our target, then push those popped elements back. This is an O(N) operation per element, leading to an overall O(N²) time complexity—perfect for the requirement.
Here's the step-by-step breakdown:
- Start with empty stack C: We'll build
Cfrom the bottom up because stack operations are LIFO (last-in, first-out). That means we need to add the element that should be the bottom ofCfirst, then work our way up to the top. - Iterate over A from last index to first: The last index in
Acorresponds to the bottom ofC, so we start there. - Retrieve the target element from B:
- For each
A[i] = k, pop the topk-1elements fromBand store them in a temporary stack. - The top element of
Bis now our target—pop it and push it toC. - Push all elements from the temporary stack back to
Bto restore its order (minus the element we just moved toC).
- For each
- Finish: Once we've processed all elements,
Cwill be in the correct order.
Here's a straightforward pseudocode version of this algorithm:
function buildStackC(A, B): N = length(A) C = empty stack # Build C from bottom to top by iterating A in reverse for i from N-1 down to 0: k = A[i] # 1-based position from top of B temp_stack = empty stack # Pop k-1 elements to reach the target for j in 1 to k-1: temp_stack.push(B.pop()) # Grab the target and add to C target_element = B.pop() C.push(target_element) # Restore B's order while temp_stack is not empty: B.push(temp_stack.pop()) return C
- Time Complexity: For each of the N elements, we perform up to N pop/push operations (in the worst case, we have to pop almost all elements to get to the target). This gives us O(N²) total time, which meets the problem's requirement.
- Space: We use a temporary stack that can hold up to N elements, but if the problem strictly prohibits extra space, we could modify this to use
Ctemporarily (though that adds some complexity). For most interview scenarios, the temp stack is acceptable. - Correctness: By restoring
Bafter each retrieval, we ensure that future operations can correctly access the remaining elements. Since all elements are distinct, we never run into conflicts when retrieving targets.
内容的提问来源于stack exchange,提问作者RonH

