2023年12月2日 星期六

[2225] Find Players With Zero or One Losses

竟然沒有超時!!!真是不敢相信我的眼睛@@!!!

/**
* 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** findWinners(int** matches, int matchesSize, int* matchesColSize, int* returnSize, int** returnColumnSizes) {
int *show = calloc (100001, sizeof(int));
int *wins = calloc (100001, sizeof(int));
int *lose = calloc (100001, sizeof(int));

for (int i=0;i<matchesSize; i++)
{
int w = matches[i][0];
int l= matches[i][1];
show[w]++;
show[l]++;
wins[w]++;
lose[l]++;
}
int w_idx=0;
int l_ldx=0;

int **ret= calloc(2, sizeof(int*));
ret[0]= calloc (100001, sizeof(int));
ret[1]= calloc (100001, sizeof(int));
for (int i=1;i<100001;i++)
{
if (show[i]>0 && lose[i]==0)
ret[0][w_idx++]= i;
if (show[i]>0 && lose[i]==1)
ret[1][l_ldx++]= i;
}
*returnSize =2;
(*returnColumnSizes) = calloc (2, sizeof(int));
returnColumnSizes[0][0]=w_idx;
returnColumnSizes[0][1]=l_ldx;
return ret;
}

沒有留言:

張貼留言