2024年2月7日 星期三

[2966] Divide Array Into Arrays With Max Difference (TBD)

ㄜ... 寫出來似乎並沒有很快!
看了一下別人的寫法,好像用counting sort 去算每個數字出現幾次,也可以解決這題?!
先貼個原本解法: 
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/


int comp (const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int** divideArray(int* nums, int numsSize, int k, int* returnSize, int** returnColumnSizes) {

qsort(nums, numsSize, sizeof(int), comp);
*returnSize= numsSize/3;
int **ret = calloc (*returnSize, sizeof(int*));
*returnColumnSizes = calloc (*returnSize , sizeof(int));
int idx=0;

for (int i=0; i<(*returnSize); i++)
{
ret[i]= calloc (3, sizeof(int));
(*returnColumnSizes)[i]=3;
idx=i*3;
if ( (nums[idx+2]-nums[idx])>k )
{
*returnSize = 0;
free(ret);
return NULL;
}
for (int j=0; j<3; j++)
ret[i][j]= nums[idx+j];

}
return ret;
}


counting sort 就先列為TBD 好了XDXD!!!


沒有留言:

張貼留言