2712
This commit is contained in:
parent
572479248d
commit
c69a6b5ca9
15
include/solution/2712.h
Normal file
15
include/solution/2712.h
Normal file
@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/27.
|
||||
//
|
||||
|
||||
#ifndef INC_2712_H
|
||||
#define INC_2712_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
long long minimumCost(char *s);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // INC_2712_H
|
18
src/2712.c
Normal file
18
src/2712.c
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/27.
|
||||
//
|
||||
#include <solution/2712.h>
|
||||
#include <string.h>
|
||||
#define min(a, b) (a < b) ? (a) : (b)
|
||||
|
||||
long long minimumCost(char *s)
|
||||
{
|
||||
int n = strlen(s);
|
||||
long long ans = 0;
|
||||
|
||||
for (int i = 1; i < n; i++)
|
||||
if (s[i] != s[i - 1])
|
||||
ans += min(i, n - i);
|
||||
|
||||
return ans;
|
||||
}
|
19
tests/test_2712.cpp
Normal file
19
tests/test_2712.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/2712.h>
|
||||
//
|
||||
// Created by xfj12 on 2025/3/27.
|
||||
//
|
||||
TEST(MinimumCostTest, Test1)
|
||||
{
|
||||
char s[] = "0011";
|
||||
long long expected = 2;
|
||||
EXPECT_EQ(minimumCost(s), expected);
|
||||
}
|
||||
|
||||
// 测试用例 2
|
||||
TEST(MinimumCostTest, Test2)
|
||||
{
|
||||
char s[] = "010101";
|
||||
long long expected = 9;
|
||||
EXPECT_EQ(minimumCost(s), expected);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user