Algorithms for Work: Data Type - Small Things Matter.
tl;dr
It's about space complexity. Use what you need.
Let start with Java.
We create many data types to store values and represent something in life.
For true or false, we have booleans.
For numbers we have byte, short, int, and long. I have to stop here and ask: "Why we need 4 type for just integer number? We even had float and double."
And look at it—it's different in size. byte (1), short (2), int (4), and long (8). Those numbers can be different when it comes to really big problems. The size matters. It's not when the problem is small, but when the problem grows bigger, for example, when you store the score of students with the type of long (8). For the class with 100 students, you spend 8B * 100 = 800B, which is really small.
Compared to using a byte to store, it's 1B * 100 = 100B. Yeah it's small. But it's 8 times larger than the minimum option. So imagine the case when this applies to 8T of data. If you select the minimum data type option, you already save 7T if you do so.
You can call it "b*ll sh*t" for this simple example. But when you're doing an actual problem, it's easy to select int to store data (even when it doesn't use all the value ranges of short or byte). When you create a DataBase table, you overexpand the use of datatypes. This example above can happen even worse.
So that's why small thing matter. You learn about algorithms and understand the basics of them.
Normally, when you solve a problem, they give you 2MB of stack and 256MB of heap.
If you select the optimal data type, you can expand your limit 2, 4, 8 times. Or even more of you apply some compression solution or this kind of idea when doing with bit (the idea of SnowFlake).
It's important when you have limited resources, so be mindful of it.
More ideas come from it: Collection / Wrapper.
Eventually, all the data comes back to be a set of data, and action comes with it. We are using the knowledge of other developers and sometime/somehow forgot why we use it; it's become the norm. It's okay, but still, we need to know the basic mindset behind it.
领英推荐
You can make use of HashMap, Queue, Stack, Heap... or everything! They make a really good version of it for us to re-use.
But, but, but...
From time to time, we use a framework and make our own class. Remember to have a checklist and remind yourself about data size, it's important.
One day, you will be the one who makes the base for the feature/ system, and let's practice from now on.
We only cover datatypes today; the BigO about time complexity will come later.
Helpful resource: