This commit is contained in:
Jeffrey Hsu 2025-03-25 17:08:39 +08:00
parent aabf07d9ec
commit 59f589894b
6 changed files with 190 additions and 26 deletions

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.30)
project(leetcode C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include)
@ -35,11 +36,6 @@ FetchContent_Declare(
FetchContent_MakeAvailable(googletest)
file(GLOB SRC_FILES "src/*.c")
foreach (SRC_FILE ${SRC_FILES})
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
add_executable(${EXE_NAME} ${SRC_FILE})
endforeach ()
add_library(solution_2711 STATIC src/2711.c)
add_subdirectory(tests)

16
include/solution/2711.h Normal file
View File

@ -0,0 +1,16 @@
//
// Created by xfj12 on 2025/3/25.
//
#ifndef INC_2711_H
#define INC_2711_H
#ifdef __cplusplus
extern "C"
{
#endif
int **differenceOfDistinctValues(int **grid, int gridSize, int *gridColSize, int *returnSize,
int **returnColumnSizes);
#ifdef __cplusplus
}
#endif
#endif // INC_2711_H

76
src/2711.c Normal file
View File

@ -0,0 +1,76 @@
#include <assert.h>
#include <solution/2711.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int differentElementNum(int *elem, int size)
{
int sum = 0;
int *temp = NULL;
for (int i = 0; i < size; i++)
{
bool flag = true;
// check same elem
for (int j = 0; j < sum; j++)
if (elem[i] == elem[j])
flag = false;
// if not in temp, add to temp
if (flag)
{
temp = realloc(temp, ++sum * sizeof(int));
temp[sum - 1] = elem[i];
}
}
free(temp);
return sum;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int **differenceOfDistinctValues(int **grid, int gridSize, int *gridColSize, int *returnSize, int **returnColumnSizes)
{
int **answer = (int **)malloc(sizeof(int *) * gridSize);
for (int r = 0; r < gridSize; r++)
{
answer[r] = (int *)malloc(sizeof(int) * gridColSize[r]);
for (int c = 0; c < gridColSize[r]; c++)
{
// calc topLeft array
int *topLeftElements = NULL;
int topLeftSize = 0;
int topLeft = 0;
for (int x = r - 1, y = c - 1; x >= 0 && y >= 0; x--, y--)
{
// add element to array
topLeftElements = realloc(topLeftElements, ++topLeftSize * sizeof(int));
topLeftElements[topLeftSize - 1] = grid[x][y];
}
topLeft = differentElementNum(topLeftElements, topLeftSize);
// calc bottomRight array
int *bottomRightElements = NULL;
int bottomRightSize = 0;
int bottomRight = 0;
for (int x = r + 1, y = c + 1; x < gridSize && y < gridColSize[r]; x++, y++)
{
bottomRightElements = realloc(bottomRightElements, ++bottomRightSize * sizeof(int));
bottomRightElements[bottomRightSize - 1] = grid[x][y];
}
bottomRight = differentElementNum(bottomRightElements, bottomRightSize);
answer[r][c] = abs(topLeft - bottomRight);
free(bottomRightElements);
free(topLeftElements);
}
}
*returnSize = gridSize;
*returnColumnSizes = gridColSize;
return answer;
}

View File

@ -1,7 +1,5 @@
file(GLOB SRC_FILES "*.cpp")
add_executable(test_2711 test_2711.cpp)
target_link_libraries(test_2711 PUBLIC gtest_main solution_2711)
foreach(SRC_FILE ${SRC_FILES})
get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE)
add_executable(${EXE_NAME} ${SRC_FILE})
target_link_libraries(${EXE_NAME} gtest gmock)
endforeach()
enable_testing()
add_test(NAME test_2711 COMMAND test_2711)

92
tests/test_2711.cpp Normal file
View File

@ -0,0 +1,92 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <gtest/gtest.h>
#include <solution/2711.h>
class DifferenceOfDistinctValuesTest : public ::testing::Test
{
protected:
void SetUp() override
{
}
void TearDown() override
{
}
};
TEST_F(DifferenceOfDistinctValuesTest, HandlesExpectedOutput)
{
int gridSize = 6;
int gridColSize[] = {10, 10, 10, 10, 10, 10};
int *grid[6];
int data[6][10] = {{6, 28, 16, 15, 35, 19, 38, 42, 32, 27}, {8, 49, 38, 16, 6, 9, 12, 11, 10, 6},
{16, 32, 46, 17, 15, 29, 5, 47, 48, 33}, {25, 12, 34, 21, 8, 15, 3, 3, 7, 38},
{32, 23, 9, 30, 35, 27, 10, 5, 12, 18}, {11, 2, 48, 22, 43, 26, 25, 1, 20, 19}};
for (int i = 0; i < gridSize; i++)
{
grid[i] = data[i];
}
int returnSize;
int *returnColumnSizes;
int **result = differenceOfDistinctValues(grid, gridSize, gridColSize, &returnSize, &returnColumnSizes);
int expected[6][10] = {{5, 5, 4, 5, 5, 4, 3, 2, 1, 0}, {4, 3, 3, 2, 3, 3, 2, 1, 0, 1},
{3, 2, 1, 1, 2, 1, 1, 0, 1, 2}, {2, 1, 0, 1, 1, 0, 1, 1, 2, 3},
{1, 0, 1, 2, 3, 3, 1, 3, 3, 4}, {0, 1, 2, 3, 4, 5, 5, 3, 5, 5}};
ASSERT_EQ(returnSize, gridSize);
for (int i = 0; i < gridSize; i++)
{
ASSERT_EQ(returnColumnSizes[i], 10);
for (int j = 0; j < 10; j++)
{
ASSERT_EQ(result[i][j], expected[i][j]);
}
}
for (int i = 0; i < returnSize; i++)
{
free(result[i]);
}
free(result);
free(returnColumnSizes);
}
TEST_F(DifferenceOfDistinctValuesTest, HandlesSmallGrid)
{
int gridSize = 3;
int gridColSize[] = {3, 3, 3};
int *grid[3];
int data[3][3] = {{1, 2, 3}, {3, 1, 5}, {3, 2, 1}};
for (int i = 0; i < gridSize; i++)
{
grid[i] = data[i];
}
int returnSize;
int *returnColumnSizes;
int **result = differenceOfDistinctValues(grid, gridSize, gridColSize, &returnSize, &returnColumnSizes);
int expected[3][3] = {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}};
ASSERT_EQ(returnSize, gridSize);
for (int i = 0; i < gridSize; i++)
{
ASSERT_EQ(returnColumnSizes[i], 3);
for (int j = 0; j < 3; j++)
{
ASSERT_EQ(result[i][j], expected[i][j]);
}
}
for (int i = 0; i < returnSize; i++)
{
free(result[i]);
}
free(result);
free(returnColumnSizes);
}

View File

@ -1,14 +0,0 @@
//
// Created by aurora on 2024/9/16.
//
#include <gtest/gtest.h>
#include <solution/338.h>
TEST(CountingBits, 1)
{
int size = 0;
int ans[] = {0, 1, 1};
EXPECT_EQ(ans, ans);//countBits(2, &size));
}
int main(){return 0;}