Gamified challenge · Hidden Markov models
The weather on each day is a hidden state: 0 = S (sunny), 1 = R (rainy) and, when there are three states, 2 = C (cloudy). observations has one letter per day: U (umbrella) or N (no umbrella). Chances are given in whole tenths (7 means 0.7): start[s] for day 1, trans[a][b] for weather a followed by b, and emit[s][0] / emit[s][1] for an umbrella / no umbrella on an s day.
A weather sequence w scores start[w1]·emit[w1][o1] · trans[w1][w2]·emit[w2][o2] · …, an exact integer (at most 10^18 for the 9-day maximum: use long in Java). Write viterbi(observations, start, trans, emit) returning the highest-scoring sequence as a string such as "SSRR". Ties go to the earlier state on the last day, then on the day before, and so on (what Viterbi does when it keeps the first best predecessor and the first best final state). An empty diary gives "".
Example: observations = "UUNUN", start = [6, 4], trans = [[7, 3], [4, 6]], emit = [[1, 9], [8, 2]] gives "RRSRS".
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google