How to solve this problem in less than O(n^2) time?

Revision en2, by gagannagpal68, 2024-03-16 16:09:10

Problem statement: You are a given a string s , int x and int y; S consists of three characters '0', '1', and '?' You can replace '?' with either '0' or '1'. You need to minimize the following equation:

Equation is a*x + b*y. 

Here a is the number of "01" subsequences and b is number of "10" subsequences after the replacement of all '?'

Constraints

Constraints
Testcase:
Possible approaches

I was thinking in direction of DP and making it run in nlogn and some sort of convex hull emerges in my head but I don't have enough practice to make it work. Any idea folks?

Tags problem, help, subsequence, dp, greedy

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English gagannagpal68 2024-03-16 16:09:10 12
en1 English gagannagpal68 2024-03-16 16:07:54 1571 Initial revision (published)