Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

A binary string is "10110010". Flipping position i costs weight[i] = [3, 1, 4, 1, 5, 9, 2, 6]. You want the string to alternate (0101... or 1010...). What is the minimum total flipping cost?

  1. A. 6
  2. B. 25
  3. C. 7
  4. D. 1

Correct answer: A. 6

For each of the two alternating targets, sum the weights of the positions where the current character differs, and take the cheaper target. Flipping only the cheapest positions can break alternation elsewhere.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic