We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Other Concepts
- Bit Array
- Discussions
Bit Array
Bit Array
Sort by
recency
|
315 Discussions
|
Please Login in order to post a comment
I think like most people, I simply started to program, leading me to a simple interpretation of the pseudo-code that used a set to track distinct integers; of course that implementation didn't pass any of the timed test cases.
In the end, this problem is not about c++, but an understanding of algorithms. After research, I stumbled across the idea of cycle detection
https://en.wikipedia.org/wiki/Cycle_detection, and tried out the first algorithm listed.I know many people don't like it when code is shared since it kind of defeats the “test” notion, but when I get stuck, I personally am thankful for those who post so I can treat the question as a learning exercise (I spent more time researching this than coding this one):
This isn’t a perfect solution - I know I have personal coding idiosyncrasies, and it doesn’t take into account edge cases such as N == 0 or P == 0 - but it does streamline Floyd’s solution with the knowledge of the limit N and does so in under 40 lines of code.
Hopefully this inspires others to go back to the research when they get the “Time limit exceeded” on their test case(s).
Working one. No more sprawling sets.
The simplest solution, but sadly it didn't like my time