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

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

Автор lax_99, история, 6 лет назад, По-английски

Hello Codeforces community!

Problem Link : Link

I have a memoized solution for this problem. The issue here is that my complexity is : O(N^3), which leads me to TLE. All the editorials and blogs I have seen have used Bottom up approach and suffix sum to get it to O(N^2). But I'm not sure how to translate this idea to the memoized version.

int dpSol(int i, int level)
{
    if(i == n - 1)
        return 1;
    
    if(cache[i][level] != -1)
        return cache[i][level];
    
    if(arr[i] == 's')
    {
        int sum = 0;
        for(int j = 0; j <= level; ++j)
            sum = (sum + dpSol(i + 1, j)) % MOD;
        return cache[i][level] = sum;
    }
    return cache[i][level] = dpSol(i + 1, level + 1);
}

Any help will be appreciated! Thanks in advance.

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

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

Can you link the problem?

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

You can't do it easily with recursion. Some people did it but it was quite hard to explain compared with the bottom up solution. If you still want the recursion solution see: 33679063