← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2026 · 考試 · 第 14 題

(4 points) A queue QQ is implemented using two stacks SinS_{\text{in}} and SoutS_{\text{out}} as follows. If each Push\textsc{Push} or Pop\textsc{Pop} with the stack has a cost of Θ(1)\Theta(1), what is the amortized cost per operation over a sequence of mm legal queue operations, starting from an empty state with the first operation being Enqueue\textsc{Enqueue}?

ENQUEUE(Q, x):
1   PUSH(Q.S_in, x)

DEQUEUE(Q):
1   if Q.S_out is not empty
2       return POP(Q.S_out)
3   while Q.S_in is not empty
4       PUSH(Q.S_out, POP(Q.S_in))
5   return POP(Q.S_out)
  • (A)
    Θ(1)\Theta(1)
  • (B)
    Θ(lg⁡m)\Theta(\lg m)
  • (C)
    Θ(m)\Theta(m)
  • (D)
    Θ(m2)\Theta(m^2)
  • (E)
    None of the other choices.

標準解析

AI 已複查