Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
It is slower (in regards to time complexity) in some specific cases, compared to solution where we compare each element to the saved value and then return instanly when the mismatch is found. In this solution you always have to loop through the whole array at least once.
It seems that the issue here is that "last" is a reference, but the result of ".collect<Vec>()" is a temporary, so in the next invocation of the closure last would be referencing a Vec from a previous closure's stack-frame which would probably blow-up, and Rust is protecting you from the creating the corruption. Happy coding!
Just for fun XD
This is elegant and clear.
But if the list is very large or if k is very large, this gets out of hand pretty quickly.
I believe the time complexity of finding all combinations is O(n^min{k, n - k}).
So if ls.len() is 1000 and k is 3, we have O(n^997) which is completely unmanageable.
I ran this on my computer vs a recursive approach,
with 1000 elements and k = 3, your algorithm took 4.13 seconds, while recursive algorithm took 0.12 seconds.
With 10,000 elements and k = 3, your algorithm took 1.25 HOURS, while recursive took 113.65 seconds.
Please consider efficiency when voting for "Best Practices". I don't mean to disparage your code -- it really is beautiful and easy to read. But it is very inefficient.
A very elegant solution. Nice work
very hard to read
Really nice! I wouldn't have thought to do it that way, very smart.
This passes over the string multiple times (each time
matches
is called). DGolubets's solution is more efficientThis comment is hidden because it contains spoiler information about the solution
A little easy for a 4 kyu maybe? I can see this being a solid 5 kyu though.
Thank you for the kind words! I'm glad you found it clear and that it helped you :)
I agree decompressing
winner?
would be more readable. I don't always worry about readability when I submit to this site though.Tbh, if I did worry about readability, I don't think my variable names would change all that much.
This was my second try. I did it from memory trying to recreate the top-rated solution because my first attempt wasn't great. Took me a while but I got it!
I'm wondering if you could explain how you got to this answer algebraically? I spent all day on it, looking at various patterns, and eventually had to give up. There's only 3 solutions to this kata, so I'm wondering if maybe it should be considered more difficult.
If I wanted to learn how to do this kind of problem better, what mathematical concepts would I need to familiarize myself with?
Very beautifully written, and easy to follow. I can learn a lot from your solutions and clarity.
Loading more items...