Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя thakurdivyanshu

Автор thakurdivyanshu, история, 4 года назад, По-английски

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.

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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