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

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

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

Hello I wanted to know how can this java code can be improved. Currently it get TLE in few test cases.It would be helpful if someone can find the bottleneck in this solution.

Problem: https://cses.fi/problemset/task/1192
Solution: find the number of connected components.

code link: https://cses.fi/paste/b4d685acb261aaca26c573/

equivalent code in c++ is 10 times faster than java.

I am trying to switch to java for cp.

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

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

I think your code could benefit from the optimizations in this blog on optimizing deep recursion in Java Additionally, you could probably use BitSets instead of Boolean arrays as they tend to be faster as far as I know.

Thanks for sharing this problem. Please let me know(in messages maybe) If the above optomizations work.