algorithm - Dynamic programming approach or a case that fails the greedy -
i have string of length 1 <= |s| <= 100 , k (1 <= k <= 10)
this string contains digits < k , question marks. want replace these question marks digits < k, no 2 neighboring digits being equal. string circular can't this: 1?1 or 11?.
the resulting string must lexicographically smallest one.
example input , output
input: k = 4 string = ????? output: 01012 i've tried greedy approach fails unknown testcases. think needs dp approach couldn't figure out states, , pure recursion code won't fit in time.
any dp approach, or tricky test cases fail greedy?
thanks,
if have digit @ 1 end of string, greedy algorithm give right answer.
if string starts , ends question mark, have 2 possibilites first character (0 or 1), run greedy algorithm on both cases , take best.
wrong answer pointed out likao:
the greedy works must start first question mark after known digit.
Comments
Post a Comment