← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2025 · 考試 · 第 1 題

考科演算法

(5 points) Consider the following randomized algorithm, which prints out some number of exclamation marks (!) and some number of asterisks (*). Assume that the size of AA is nn initially. What are the expected numbers of !s and *s that the algorithm prints out? Choose the tightest possible answers.

RandomizedPrint(A):
  n = length(A)
  if n <= 1:
    print "!"
    return
  for i in {0,...,n-1}:
    print "*"
  Choose a uniformly random integer p in {1,...,n-1}
  RandomizedPrint(A[:p])
  RandomizedPrint(A[p:])
  • (A)
    !: O(log⁡n)O(\log n), *: O(nlog⁡n)O(n \log n)
  • (B)
    !: O(log⁡n)O(\log n), *: O(n2)O(n^2)
  • (C)
    !: O(n)O(n), *: O(nlog⁡n)O(n \log n)
  • (D)
    !: O(n)O(n), *: O(n2)O(n^2)
  • (E)
    !: O(nlog⁡n)O(n \log n), *: O(n2)O(n^2)

標準解析

AI 已複查