CPSC 320 Final - Intermediate Algorithm Design and Analysis
Here are two questions that I remember from the CPSC320 Final.
Divide and conquer
Majority element
Design an divide and conquer algorithm to determine whether an integer x is a majority element (appears more than len(A)/2 of times) of a sorted integer array A.
Given a greedy algorithm:
Start from the beginning and work to the end, once we find a substring that can be splitted, split after it.
Does this algoritm work?
No. The greedy algorithm doesn’t work.
If we have valid words:
“a”, “bb”, “bbb”
Can we split “bbba”?
Greedy will split like this: “bb”
“ba” (“ba” is not word)
but actually can split: “bbb”
“a”
Give a DP solution to the problem
Recurrence:
canSplit(str) := True If isWord(str)
:= isWord(str[:i]) and canSplit(str[i:]) Otherwise