2974
This commit is contained in:
parent
a09362480a
commit
93f367dee9
18
include/solution/2974.h
Normal file
18
include/solution/2974.h
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/25.
|
||||
//
|
||||
|
||||
#ifndef INC_2974_H
|
||||
#define INC_2974_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
/**
|
||||
* Note: The returned array must be malloced, assume caller calls free().
|
||||
*/
|
||||
int *numberGame(int *nums, int numsSize, int *returnSize);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // INC_2974_H
|
52
src/2974.c
Normal file
52
src/2974.c
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
38
tests/test_2974.cpp
Normal file
38
tests/test_2974.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/2974.h>
|
||||
|
||||
TEST(NumberGameTest, Test1)
|
||||
{
|
||||
int nums[] = {5, 4, 2, 3};
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int returnSize = 0;
|
||||
|
||||
int *result = numberGame(nums, numsSize, &returnSize);
|
||||
|
||||
int expected[] = {3, 2, 5, 4};
|
||||
ASSERT_EQ(returnSize, 4);
|
||||
for (int i = 0; i < returnSize; ++i)
|
||||
{
|
||||
EXPECT_EQ(result[i], expected[i]);
|
||||
}
|
||||
|
||||
free(result);
|
||||
}
|
||||
|
||||
TEST(NumberGameTest, Test2)
|
||||
{
|
||||
int nums[] = {2, 5};
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int returnSize = 0;
|
||||
|
||||
int *result = numberGame(nums, numsSize, &returnSize);
|
||||
|
||||
int expected[] = {5, 2};
|
||||
ASSERT_EQ(returnSize, 2);
|
||||
for (int i = 0; i < returnSize; ++i)
|
||||
{
|
||||
EXPECT_EQ(result[i], expected[i]);
|
||||
}
|
||||
|
||||
free(result);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user