Data structure Wangdao postgraduate entrance examination book sequence table unified test questions--- find the smallest positive integer that does not appear in the array

Topic description: Given an array of n (n ≥ 1) integers, please design a time-efficient algorithm to find the smallest positive integer that does not appear in the array. For example, the smallest positive integer that does not appear in the array {-5,3,2,3} is 1; the smallest positive integer that does not appear in the array {1,2,3} is 4.

The answer is to exchange space for time, the time complexity is O(n), and the space complexity is O(n). The answer is very clear. I want to record another way of thinking.

idea one

  1. Set a k, the initial value is 1, and then traverse the array to find out whether there is an element equal to k, if found, ++k, and reset i to 0 at this time (because the array is unordered, Still need to start from scratch)
  2. If k is not found for the entire array, the positive integer that does not appear is k
  3. The time complexity is O(n^2) and the space complexity is O(1)

The specific code is as follows (runable):

#include<stdio.h>

// 查找未出现的最小正整数的函数
int find(int a[],int n){
    
    
	int k,i,c;
	k = 1;
	for(i=0;i<n;i++) {
    
    
		if(a[i] == k){
    
    
				++k;
				i=0;// 其实每次找到后a[i]后,将i重置为0,也就相当于多了一层循环
		}
	}
	return k;
}

int main() {
    
    
	int a[100],i,n,res;
	printf("请输入数组个数:");
	scanf("%d",&n);
	printf("请输入每个元素的值:\n");
	for(i=0;i<n;i++){
    
    
		scanf("%d",&a[i]);
	} 
	
	res = find(a,n);
	printf("最大正整数是: %d\n",res);
	return 0;
}

idea two

  1. The second idea is basically the same as the idea, except that quick sort is added, and the array is ordered first.
  2. Using quick sort, you can make the array orderly, so you don't need to reset i every time you find an element like the above idea
  3. The time complexity is O(nlogn) and the space complexity is O(1)
#include<stdio.h>

// 快速排序
void QuickSort(int arr[], int low, int high)
{
    
    
    if (low < high)
    {
    
    
        int i = low;
        int j = high;
        int k = arr[low];
        while (i < j)
        {
    
    
            while(i < j && arr[j] >= k)     // 从右向左找第一个小于k的数
            {
    
    
                j--;
            }
 
            if(i < j)
            {
    
    
                arr[i++] = arr[j];
            }
 
            while(i < j && arr[i] < k)      // 从左向右找第一个大于等于k的数
            {
    
    
                i++;
            }
 
            if(i < j)
            {
    
    
                arr[j--] = arr[i];
            }
        }
 
        arr[i] = k;
 
        // 递归调用
        QuickSort(arr, low, i - 1);     // 排序k左边
        QuickSort(arr, i + 1, high);    // 排序k右边
    }
}

// 查找未出现的最小正整数的函数
int find(int a[],int n){
    
    
	int k,i,c;
	k = 1;
	QuickSort(a, 0, n-1);
	for(i=0;i<n;i++) {
    
    
		if(a[i] == k){
    
    
				k++;
			} else {
    
    
				break;
			}
		}
	c = k;
	return c;
}

int main() {
    
    
	int a[100],i,n,res;
	printf("请输入数组个数:");
	scanf("%d",&n);
	printf("请输入每个元素的值:\n");
	for(i=0;i<n;i++){
    
    
		scanf("%d",&a[i]);
	} 
	
	res = find(a,n);
	printf("最大正整数是: %d\n",res);
	return 0;
}

In fact, it can be seen that when the array is ordered, it will make the problem much simpler, and the time complexity is the time complexity of sorting. (Including the same feeling when writing other topics). When I was a freshman, I didn't understand why sorting was so important, but now I understand that it makes sense to continuously improve the sorting algorithm to make the time complexity as small as possible!

Guess you like

Origin blog.csdn.net/weixin_47505105/article/details/123459971