[Zero Basic Algorithms Lecture 2] The Space Complexity of Algorithms

There are some space limitations in the process of doing the questions, the "memory limit" as shown in the figure below is 128MB. This 128MB limits that if we do not open the stack, do not recurse, and only open the int array, we can only open the array to 1 0 7 10^7107 this order of magnitude. How did you get this number?
Insert picture description here

Pre-knowledge

Several units of memory size

The smallest unit of the memory is a wire. We can use power to represent 1 and power off to represent 0, that is, a wire can only represent two numbers, so the memory uses binary. We call a wire a binary bit (1 bit)

The units that indicate the memory size are: bit, Byte, KB, MB, GB, TB

Some of the units are explained as follows:

unit meaning name Convert
bit A binary bit Bit
Byte Eight binary bits byte 8 bit = 1 Byte
KB 1024 bytes Kilobytes 1024 Byte = 1 KB
MB Units one level higher than KB Megabyte 1024 KB = 1 MB

Just as the unit of time is hour , minute and second , after the second is rounded up, it is the minute, and after the minute rounded up is the hour. Our memory unit also has a corresponding carry relationship. Except that bit to Byte are octal, the rest are 2 10 = 1024 2^{10} = 1024210=1024

Common numeric types

type of data A memory occupied by a variable of this type Storage method The largest number that can be represented (magnitude)
int 32 bits (32 bit) The first binary bit is the sign bit, this bit is 0 for positive number, 1 for negative number, and the remaining positions store actual numbers 2 31 − 1 = 2147483647 2^{31} - 1 = 2147483647 2311=2147483647
long long 64 bit binary bit (64bit) Same as above 2 64 − 1 2^{64} - 1 2641
double 64-bit binary Storage in scientific notation. The first binary bit is the sign bit, and the next 11 bits store the exponent part of 10 in scientific notation. The remaining 53 digits store the decimal part (see diagram below) 1 0 308 10^{308} 10308

Storage method of double type:
Insert picture description here

Calculate the allowable array size based on space constraints

Space limit is 512 MB 512MB5 1 2 M B , if the stack is not opened, no recursion, and only an int array is opened, how large can this array be opened?

First convert MB to bit: the space limit is 512 × 1024 × 1024 × 8 512\times1024\times1024\times8512×1024×1024×8 bit

Then we know that an int type occupies 32 bits, so the allowed array size is: 512 × 1024 × 1024 × 8 32 = 128 × 1024 × 1024 ≈ 1 0 8 \frac{512\times1024\times1024\times8}{32} = 128\times1024\times1024 ≈10^{8}32512×1024×1024×8=128×1024×1024108

Practice questions

Space limit is 128 MB 128MB1 2 8 M B time, if you do not open the stack, no recursion, and only a double array, the array can be opened to much of the order?

Length is 1 0 7 10^710How much space does the double type array of 7 occupy? (Just say the order of magnitude)

See you in the comments section

Guess you like

Origin blog.csdn.net/weixin_44559752/article/details/108237422