Data Structures - Space Complexity

insert image description here
3. Space complexity

Space complexity is also a mathematical expression, which is a measure of the amount of storage space temporarily occupied by an algorithm during operation.
Space complexity is not how many bytes the program occupies, because this is not very meaningful, so space complexity is calculated by the number of variables.
The space complexity calculation rules are basically similar to the practical complexity, and the big O asymptotic notation is also used.
Note: The stack space (storage parameters, local variables, some register information, etc.) required by the function at runtime has been determined during compilation, so the space complexity is mainly determined
by the extra space explicitly requested by the function at runtime.
Example 1:

// 计算BubbleSort的空间复杂度?
void BubbleSort(int* a, int n)
{
    
    
	assert(a);
	for (size_t end = n; end > 0; --end)
	{
    
    
		int exchange = 0;
		for (size_t i = 1; i < end; ++i)
		{
    
    
			if (a[i - 1] > a[i])
			{
    
    
				Swap(&a[i - 1], &a[i]);
				exchange = 1;
			}
		}
		if (exchange == 0)
			break;
	}
}

The above bubble sorting we said in the last article that the time complexity is O(N^2), and the time complexity is actually O(1), which is similar to the big O asymptotic method we talked about before. Let’s look at the program created The variables are all constant terms, so it is O(1).

Space complexity must remember a rule that space is not accumulated, but time is accumulated.

// 计算Fibonacci的空间复杂度?
// 返回斐波那契数列的前n项
long long* Fibonacci(size_t n)
{
    
    
	if (n == 0)
		return NULL;
	long long* fibArray = (long long*)malloc((n + 1) * sizeof(long long));
	fibArray[0] = 0;
	fibArray[1] = 1;
	for (int i = 2; i <= n; ++i)
	{
    
    
		fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
	}
	return fibArray;
}

This is an iteration of Fibonacci, so the time complexity is O(N), and the space complexity is also O(N), because our malloc has opened up space.

long long Fac(size_t N)
{
    
    
	if (N == 0)
		return 1;
	return Fac(N - 1) * N;
}

Everyone may think that this space complexity is O(2^n), but it is actually O(N), because the function stack frame creation will be destroyed, and there is a lot of space reuse, which is why we say that space is not accumulated, but Time is cumulative.
4. Common complexity comparison
The common complexity of general algorithms is as follows:
insert image description here
Generally, the latter few of our algorithms will not be used, and they are too slow.
Here are a few oj questions for everyone to do

topic one

Idea 1

We can use the idea of ​​​​hashing, that is, first have an array, the contents of which are initialized to -1, and then put the number in the corresponding array, and then traverse the array, if it is -1, then we The value to look for.

int missingNumber(int* nums, int numsSize){
    
    
        int*num=(int*)malloc(sizeof(int)*(numsSize+1));
        int i=0;
        for(i=0;i<=numsSize;i++)
        {
    
    
            num[i]=-1;
        }
        for(i=0;i<numsSize;i++)
        {
    
    
           num[nums[i]]=nums[i];
        }
        for(i=0;i<=numsSize;i++)
        {
    
    
            if(num[i]==-1)
            return i;
        }
        free(num);
        return NULL;
}

insert image description here
That's the way of thinking.
insert image description here
When I first wrote it, I kept adjusting the compilation error. In fact, there is a missing return value. You can put it on VS for debugging, just like this for a main function.
insert image description here

#include<stdio.h>
#include<stdlib.h>
int missingNumber(int* nums, int numsSize) {
    
    
    int* num = (int*)malloc(sizeof(int) * (numsSize + 1));
    int i = 0;
    for (i = 0; i <= numsSize; i++)
    {
    
    
        num[i] = -1;
    }
    for (i = 0; i < numsSize; i++)
    {
    
    
        num[nums[i]] = nums[i];
    }
    for (i = 0; i <= numsSize; i++)
    {
    
    
        if (num[i] == -1)
            return i;
    }
    free(num);
    return NULL;
}
int main()
{
    
    
	int arr[] = {
    
     2,3,4,0 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    int ret=missingNumber(arr, sz);
    printf("%d", ret);
	return 0;
}

Idea 2

Bitwise XOR, this is a very fast idea. Because our 0 and any number XOR are themselves, then we only need to give a 0, and then because the same number XOR is 0, then look at our code.

int missingNumber(int* nums, int numsSize){
    
    
        int x=0;
        for(int i=0;i<numsSize;i++)
        {
    
    
            x^=nums[i];
        }
        for(int i=0;i<=numsSize;i++)
        {
    
    
            x^=i;
        }
        return x;
}

In fact, there are still ideas, but I won't write them. Give me an idea.
Idea 3, first sort, then search, and traverse one by one in order, but the space complexity is definitely not O(N), because sorting takes time.
Idea 4, add the numbers from 0 to N, and then subtract this array, you get the number that disappears.

Number of spins

void revolve(int*left,int*right)
{
    
    
    while(left<right)
    {
    
    
        int tmp=*left;
        *left=*right;
        *right=tmp;
        left++;
        right--;
    }
}


void rotate(int* nums, int numsSize, int k){
    
    
        if(numsSize==1)
        return ;
        k=k%numsSize;
        revolve(nums,nums+numsSize-1);
        revolve(nums,nums+k-1);
        revolve(nums+k,nums+numsSize-1);
}

insert image description here
That's all for today's sharing, see you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132218316
Recommended