### ruchir28's blog

By ruchir28, history, 7 weeks ago,

I am using an approach that will work within the time limit and even the same approach is accepted in C++ but gives TLE in java I have also used Fast IO but still TLE. Can anyone help me with this. Problem Link : https://codeforces.com/contest/986/problem/A Submission : https://codeforces.com/contest/986/submission/132535284

• +8

 » 7 weeks ago, # |   0 Auto comment: topic has been updated by ruchir28 (previous revision, new revision, compare).
 » 7 weeks ago, # |   0 even the same approach is accepted in C++Did you try implementing your approach in C++ then?
•  » » 7 weeks ago, # ^ |   0 No, But I have seen a similar implementation
•  » » » 7 weeks ago, # ^ |   +3 There are 2 options now: Someone optimizes your code for you. You generate test cases and profile your code. Option 1 is unlikely given how messy the code is. Moreover, there is a possibility that you might have analyzed the time complexity of your code wrongly (then it would be a waste of time for others to try to study your code).I recommend that you generate some large test cases and profile your code to see where the bottleneck in your code is.
 » 7 weeks ago, # |   0 You're using linked list and accessing random indices, therefore it becomes n^2 and is way too slow
•  » » 7 weeks ago, # ^ |   0 Hey I am using LinkedList as a queue where the operations would be just removing the first element and adding to last both are O(1)
•  » » » 7 weeks ago, # ^ |   0 You are using LinkedList for no reason instead of array/ArrayList for the goods variable.Didn't look to carefully at your code, but the complexity is probably fine. To get AC you need to make sure the constant is low as well.I got TLE in C++ because I used a multiset in the last loop instead of sorting. Also changing dist[n][k] to dist[k][n] almost halved the execution time (I assume here the cache misses really hurt the performance).So look at other java solutions that get AC and keep tweaking your solution until you get AC (my guess is that you have to ditch the LinkedList altogether).
•  » » » » 6 weeks ago, # ^ |   0 the complexity is wrong
•  » » » » » 6 weeks ago, # ^ |   0 well it's O(n*k) instead of O(n) which should still fit as k<=100
•  » » » » » » 6 weeks ago, # ^ |   0 my bad, i feel really stupid now
•  » » » » 6 weeks ago, # ^ |   0 Thanks,I have corrected the LinkedList part,Also when I saw some submissions in java for this problem I noticed that they have used a jagged array to represent their graph instead of List so I did that and it got accepted. Maybe that resize operation in Arraylist might have consumed time(for larger inputs) or something else.
•  » » » 6 weeks ago, # ^ |   0 Re-read the original comment and then read this  for(int i=0;i()); int good = s.nextInt(); good--; goods.get(good).add(i); } 
•  » » » » 6 weeks ago, # ^ |   0 Thank for pointing out I have corrected that.