Skip to content

Commit 7cef2dd

Browse files
committed
feat: add programmers-132265
1 parent 337a5c0 commit 7cef2dd

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

coding-test-cpp-practice/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ add_subdirectory(problem43163)
66
add_subdirectory(problem43164)
77
add_subdirectory(problem64063)
88
add_subdirectory(problem72411)
9+
add_subdirectory(problem132265)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
enable_testing()
2+
find_package(GTest REQUIRED)
3+
4+
if(WIN32)
5+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
6+
endif(WIN32)
7+
8+
macro(add_library_target target)
9+
add_library(${target} STATIC "${target}.cpp")
10+
target_link_libraries(${target} PRIVATE ${ARGN})
11+
set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
12+
endmacro()
13+
14+
macro(add_target_test target)
15+
string(REPLACE "_test" "" target_name ${target})
16+
add_executable(${target} "${target}.cpp" "${target_name}.cpp")
17+
target_link_libraries(${target} PRIVATE GTest::gtest GTest::gtest_main ${ARGN})
18+
if(MSVC)
19+
### Edit and Continue for CMake projects
20+
target_compile_options(${target} PUBLIC "/Zi")
21+
target_link_options(${target} PUBLIC "/INCREMENTAL")
22+
endif()
23+
gtest_discover_tests(${target})
24+
endmacro()
25+
26+
add_library_target(cutting_roll_cake)
27+
28+
add_target_test(cutting_roll_cake_test)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "cutting_roll_cake.h"
2+
3+
#include <unordered_map>
4+
5+
int CuttingRollCake::CuttingRollCake(std::vector<int>& topping)
6+
{
7+
auto total_map = std::unordered_map<int, int>{};
8+
for (const auto& item : topping)
9+
{
10+
++total_map[item];
11+
}
12+
13+
auto left_map = std::unordered_map<int, int>{};
14+
auto same_topping_case = 0;
15+
for (auto current : topping)
16+
{
17+
++left_map[current];
18+
--total_map[current];
19+
20+
if (total_map[current] == 0)
21+
{
22+
total_map.erase(current);
23+
}
24+
25+
if (left_map.size() == total_map.size())
26+
{
27+
++same_topping_case;
28+
}
29+
}
30+
31+
return same_topping_case;
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef CODING_TEST_CPP_PRACTICE_CUTTING_ROLL_CAKE_H
2+
#define CODING_TEST_CPP_PRACTICE_CUTTING_ROLL_CAKE_H
3+
4+
#include <vector>
5+
6+
namespace CuttingRollCake
7+
{
8+
/**
9+
* @brief Find the number of cases that the left and right pieces of cake have the same topping
10+
* @param topping topping array of cake
11+
* @return the number of cases that the left and right pieces of cake have the same topping
12+
*/
13+
int CuttingRollCake(std::vector<int>& topping);
14+
}
15+
16+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "cutting_roll_cake.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
GTEST_TEST(CuttingRollCake, Successful1)
6+
{
7+
auto topping = std::vector<int>{1, 2, 1, 3, 1, 4, 1, 2};
8+
const auto expected = 2;
9+
const auto actual = CuttingRollCake::CuttingRollCake(topping);
10+
EXPECT_EQ(expected, actual);
11+
}
12+
13+
GTEST_TEST(CuttingRollCake, Successful2)
14+
{
15+
auto topping = std::vector<int>{1, 2, 3, 1, 4};
16+
const auto expected = 0;
17+
const auto actual = CuttingRollCake::CuttingRollCake(topping);
18+
EXPECT_EQ(expected, actual);
19+
}

0 commit comments

Comments
 (0)