4 ms·
No problem, even if you had it in school it takes some practice thinking this way to become fluent. A fun exercise to give reality to the theoretical is to run
by jasonpeacock 5y ago
No problem, even if you had it in school it takes some practice thinking this way to become fluent.
A fun exercise to give reality to the theoretical is to run some benchmarks for your solution, providing inputs of size 10, 100, 1K, 10K, 100K, 1M, etc. and seeing how the performance changes. You can also (with some benchmarking tools) look at the memory usage as well.
When talking about algorithm performance, there's usually considerations for both time and space (memory or disk) that need to be considered.
One thing you'll notice is that most solutions, even the O(n^2), are "fast enough" at small sizes that it doesn't matter what approach you take. In those situations, readability/clarity usually wins such as with your proposed solution.
If I knew my lists would never get larger than 100 elements (or even 1K), I would totally do what you did. The secret is in knowing the problem you're solving.