2022年11月22日 星期二

[1539] Kth Missing Positive Number(TBD)

寫了算是暴力解~~~O(n)
如果有回來optimize 再寫成O(logN)  XD
彷彿好像應該要用到binary search~
int findKthPositive(int* arr, int arrSize, int k){
int *hash=calloc(10001,sizeof(int));
for (int i=0;i<arrSize;i++)
hash[arr[i]]=1;

int count=0;
for (int i=1;i<10001;i++)
{
if (hash[i]==0)
count++;
if (count==k)
return i;
}
return 10000;
}

沒有留言:

張貼留言