Files
leetcode/src/2974.c
2025-03-26 11:58:36 +08:00

52 lines
1.1 KiB
C

//
// Created by xfj12 on 2025/3/25.
//
#include <solution/2974.h>
#include <stdio.h>
#include <stdlib.h>
int findMinElement(int *nums, int numsSize, int *returnIndex)
{
int min = nums[0];
*returnIndex = 0;
for (int i = 1; i < numsSize; ++i)
{
if (nums[i] < min)
{
min = nums[i];
*returnIndex = i;
}
}
return min;
}
void removeElement(int *nums, int numsSize, int idx)
{
for (int i = idx; i < numsSize - 1; ++i)
nums[i] = nums[i + 1];
}
int *numberGame(int *nums, int numsSize, int *returnSize)
{
int *result = malloc(sizeof(int) * numsSize);
*returnSize = 0;
while (numsSize > 0)
{
int aliceIndex;
const int alice = findMinElement(nums, numsSize, &aliceIndex);
removeElement(nums, numsSize--, aliceIndex);
int bobIndex;
const int bob = findMinElement(nums, numsSize, &bobIndex);
removeElement(nums, numsSize--, bobIndex);
result[(*returnSize)++] = bob;
result[(*returnSize)++] = alice;
}
for (int i = 0; i < numsSize; ++i)
printf("%d ", result[i]);
return result;
}