Please read the new rule regarding the restriction on the use of AI tools. ×

thakurdivyanshu's blog

By thakurdivyanshu, history, 4 years ago, In English

894A Problem Can someone help me in suggesting a way to solve this problem in O(n) time using dp??(the O(n^3) approach is quite obvious) I am newbie and trying to learn dp.

  • Vote: I like it
  • -6
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Build prefix sums on the frequency of Q. You can then calculate the number of subsequences by summing up number of prefix Q's multiplied by the number of suffix Q's for each position where A is present.

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I just read the editorial. The $$$O(N)$$$ solution is already mentioned there. Why make a blog regarding the same then? Did you not think of reading the editorial before making a post?

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

There is an editorial about this.

If we only iterate on the place of 'A', we can get the number of 'Q' before and after it using prefix sums, and it leads to O(n) solution.

If you dont know how to implement, here it is

O(n) solution