703. 數據流中的第K大元素 Kth Largest Element in a Stream

題目 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void sort_bubble(int ptr[],int size,int l){//冒泡
	int i,j,k;
	for(i=0;i<l;i++){
		for(j=size-1;j>i;j--)
		{
			if(ptr[j]>ptr[j-1])
			{
				k=ptr[j];
				ptr[j]=ptr[j-1];
				ptr[j-1]=k;
			}
		}
	}
}

void sort(int ptr[],int start,int end){//快排
	int s = start;
	int l = start+1,r = end;
	int c;
	if(l>r)	return;
	while(l<=r){
		while(l<=r&&ptr[r]<ptr[s])	r--;
		if(l>r)	break;
		c = ptr[r];
		ptr[r] = ptr[s];
		ptr[s] = c;
		s = r;
		r--;
		while(l<=r&&ptr[l]>ptr[s])	l++;
		if(l>r)	break;
		c = ptr[l];
		ptr[l] = ptr[s];
		ptr[s] = c;
		s = l;
		l++;
	}
	sort(ptr,start,s-1);
	sort(ptr,s+1,end);
}

int search(int* nums,int left,int right,int target){//二分查找
	if(left > right)
		return left;
	int middle = (left+right)/2;
	if(nums[middle] == target){
		return middle;
	}
	if(nums[middle] < target){
		return search(nums,left,middle-1,target);
	}
	else{
		return search(nums,middle+1,right,target);
	}
}

int AA[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648};


typedef struct {
	int *n;
	int size;
	int length;
} KthLargest;

KthLargest* kthLargestCreate(int k, int* nums, int numsSize) {
    KthLargest *obj = malloc(sizeof(KthLargest));
	obj->n = malloc(sizeof(int)*k);
	obj->size = k;
	obj->length = 0;
	if(k > sizeof(AA)/sizeof(int))
	{
		sort(nums,0,numsSize-1);
	}
	else
	{
		if(AA[k] < numsSize)
			sort_bubble(nums,numsSize,k);
		else
			sort(nums,0,numsSize-1);
	}
	if(numsSize < obj->size){
		memcpy(obj->n,nums,sizeof(int)*numsSize);
		obj->length = numsSize;
	}else{
		memcpy(obj->n,nums,sizeof(int)*obj->size);
		obj->length = obj->size;
	}
	return obj;
}

int kthLargestAdd(KthLargest* obj, int val) {
	int index;
    if(val > obj->n[0])
	{
		index = 0;
		goto finish;
	}

	if(val < obj->n[obj->length-1])
	{
		index = obj->length;
		goto finish;
	}
	index = search(obj->n,0,obj->length-1,val);
    finish:
	if(obj->length < obj->size){//還有空餘空間
		if(index != obj->length){//當不是最後一個 則需要移位
			memmove(&obj->n[index+1],&obj->n[index],sizeof(int)*(obj->length-index));
		}
		obj->n[index] = val;//賦值
		obj->length++;
	}else{//沒有空餘空間
		if(index != obj->size){//當不是最後一個 則需要移位 ; 最後一個則不理了 不插入
			memmove(&obj->n[index+1],&obj->n[index],sizeof(int)*(obj->size-index-1));
			obj->n[index] = val;
		}
	}
	
	return obj->n[obj->length-1];
}

void kthLargestFree(KthLargest* obj) {
	free(obj->n);
	free(obj);
}



int main(){
	{
		int k = 3;
		int nums[] = {4,5,8,2};
		int param_1;
		KthLargest* obj = kthLargestCreate(k, nums, sizeof(nums)/sizeof(int));

		param_1 = kthLargestAdd(obj, 3);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 5);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 10);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 9);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 4);
		printf("%d\n",param_1);

		kthLargestFree(obj);
	}

	{
		int k = 1;
		int nums[] = {};
		int param_1;
		KthLargest* obj = kthLargestCreate(k, nums, sizeof(nums)/sizeof(int));

		param_1 = kthLargestAdd(obj, -3);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, -2);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, -4);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 0);
		printf("%d\n",param_1);

		param_1 = kthLargestAdd(obj, 4);
		printf("%d\n",param_1);

		kthLargestFree(obj);
	}

}

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章