sdKrhMn286's blog

By sdKrhMn286, history, 19 months ago, In English

I wrote this solution for this problem and getting TLE on last test case. Can someone tell me how I can make my solution more optimal with or without DP .

Thanks in advance :);

  • Vote: I like it
  • +11
  • Vote: I do not like it

»
19 months ago, # |
  Vote: I like it +3 Vote: I do not like it
  1. Pass the string by reference in rec().
  2. Add this in the first line of rec() if((int)(ans.size()) return; because you only need one possible answer.
  3. You have not memoized your recursion. (And the dp array should be a 2D array instead, although you haven't used it).
  • »
    »
    19 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Passing the string by reference worked, thanks man.