Compare commits
2 Commits
a09362480a
...
572479248d
Author | SHA1 | Date | |
---|---|---|---|
572479248d | |||
93f367dee9 |
15
include/solution/2829.h
Normal file
15
include/solution/2829.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef INC_2829_H
|
||||||
|
#define INC_2829_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int minimumSum(int n, int k);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // INC_2829_H
|
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
|
31
src/2829.c
Normal file
31
src/2829.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Created by xfj12 on 2025/3/26.
|
||||||
|
//
|
||||||
|
#include <solution/2829.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int minimumSum(int n, int k)
|
||||||
|
{
|
||||||
|
int *KAvoid = malloc(sizeof(int) * n);
|
||||||
|
int idx = 1;
|
||||||
|
KAvoid[0] = 1;
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 2; idx < n; i++)
|
||||||
|
{
|
||||||
|
bool flag = true;
|
||||||
|
for (int j = 0; j < idx && flag; j++)
|
||||||
|
if (KAvoid[j] + i == k)
|
||||||
|
flag = false;
|
||||||
|
if (flag)
|
||||||
|
KAvoid[idx++] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
sum += KAvoid[i];
|
||||||
|
|
||||||
|
free(KAvoid);
|
||||||
|
return sum;
|
||||||
|
}
|
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;
|
||||||
|
}
|
16
tests/test_2829.cpp
Normal file
16
tests/test_2829.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/2829.h>
|
||||||
|
|
||||||
|
TEST(MinimumSumTest, Test1)
|
||||||
|
{
|
||||||
|
int n = 5, k = 4;
|
||||||
|
int expected = 18;
|
||||||
|
EXPECT_EQ(minimumSum(n, k), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(MinimumSumTest, Test2)
|
||||||
|
{
|
||||||
|
int n = 2, k = 6;
|
||||||
|
int expected = 3;
|
||||||
|
EXPECT_EQ(minimumSum(n, k), expected);
|
||||||
|
}
|
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