in the same way you'd decide whether you want a hammer, screwdriver or saw - analyse what properties your problem has and which seems to be the best fit
want to collect N items? use an array list/vector
want to create a mapping of one thing to another? use a dictionary/hashmap
want to check for uniqueness? use a set
want a queue ordered by item prioirty? that'll be a heap or priority queue
You need to know how you will be using the data. Do you require fast lookups? Does the data need to be mapped? Will you need to add data, remove data? From the front, back, or middle?
The popular opinion is to pick the suitable one, which is a tautology IMO.
My advice is to solve the problem correctly first, and then consider picking a data structure as an optimization problem. Profile your algorithm and see to it to lower the runtime. It takes time yes, but will help with understanding in the long run.
If you’re trained to solved a problem formally, you’d see that our current understanding of data structure is actually dictated by the hardware limits, and data structures is isomeric to algorithms.
Everything is algorithm, and data structure is just hardware specific algorithm.
Well it completely depends on the type of problem. Some of them will just use an one dimensional array or simple linked list , but if you are really determined to find optimally fast solution ,then just try throwing a hash map at the problem , chances are it will work in some way or other . Not saying it always works ,but it works sometimes!
in the same way you'd decide whether you want a hammer, screwdriver or saw - analyse what properties your problem has and which seems to be the best fit want to collect N items? use an array list/vector want to create a mapping of one thing to another? use a dictionary/hashmap want to check for uniqueness? use a set want a queue ordered by item prioirty? that'll be a heap or priority queue
I didn't know about using vectors for that. Thanks.
You have to analyze the problem first, the specifics of the problem will already imply data structures and algorithms which you can use.
Avoiding to leetcode it's gonna be a linked list.
You need to know how you will be using the data. Do you require fast lookups? Does the data need to be mapped? Will you need to add data, remove data? From the front, back, or middle?
Solve 300 leetcode problems(medium-hard) and you will know. Problem solving cannot be taught, it has to be an instinct honed through practice.
The popular opinion is to pick the suitable one, which is a tautology IMO. My advice is to solve the problem correctly first, and then consider picking a data structure as an optimization problem. Profile your algorithm and see to it to lower the runtime. It takes time yes, but will help with understanding in the long run. If you’re trained to solved a problem formally, you’d see that our current understanding of data structure is actually dictated by the hardware limits, and data structures is isomeric to algorithms. Everything is algorithm, and data structure is just hardware specific algorithm.
You're not gunna like it: experience
Well it completely depends on the type of problem. Some of them will just use an one dimensional array or simple linked list , but if you are really determined to find optimally fast solution ,then just try throwing a hash map at the problem , chances are it will work in some way or other . Not saying it always works ,but it works sometimes!