Skip to content

Commit

Permalink
Solved 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Sep 1, 2024
1 parent 28013e2 commit 3ff2f02
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ add_subdirectory( second-minimum-node-in-a-binary-tree )
add_subdirectory( top-k-frequent-elements )
add_subdirectory( two-sum )
add_subdirectory( valid-parentheses )
add_subdirectory( convert-1d-array-into-2d-array )
23 changes: 23 additions & 0 deletions convert-1d-array-into-2d-array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
project(convert_1d_array_into_2d_array)

add_executable(
convert_1d_array_into_2d_array
tests.cpp
)

add_executable(
convert_1d_array_into_2d_array_runner
runner.cpp
)

target_link_libraries(
convert_1d_array_into_2d_array
GTest::gtest_main
)

target_link_libraries(
convert_1d_array_into_2d_array_runner
utils
)

gtest_discover_tests(convert_1d_array_into_2d_array)
3 changes: 3 additions & 0 deletions convert-1d-array-into-2d-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 2022. Convert 1D Array Into 2D Array

[LeetCode link](https://leetcode.com/problems/convert-1d-array-into-2d-array/)
13 changes: 13 additions & 0 deletions convert-1d-array-into-2d-array/runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <vector>

#include "print_vector.hpp"

#include "solution.hpp"

int main()
{
Solution s;
std::cout << s.construct2DArray({ 1, 2, 3 }, 1, 3);
return 0;
}
25 changes: 25 additions & 0 deletions convert-1d-array-into-2d-array/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <iterator>
#include <vector>

class Solution {
public:
std::vector<std::vector<int>> construct2DArray(const std::vector<int>& original,
std::vector<int>::size_type m, std::vector<int>::size_type n) {
if (original.size() != (n * m))
return {};

std::vector<std::vector<int>> ret(m);
for (std::vector<int>::size_type i = 0; i < m; ++i) {
ret[i].reserve(n);
auto i_begin = original.begin();
std::advance(i_begin, i * n);
auto i_end = original.begin();
std::advance(i_end, (i + 1)* n);
std::copy(i_begin, i_end, std::back_inserter(ret[i]));
}

return ret;
}
};
46 changes: 46 additions & 0 deletions convert-1d-array-into-2d-array/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <gtest/gtest.h>

#include <utility>
#include <vector>

#include "solution.hpp"

typedef std::vector<int> ll;
typedef std::vector<ll> lll;
typedef ll::size_type ll_size_type;

class Convert1dTo2dArrayTest :public ::testing::TestWithParam<std::tuple<ll, ll_size_type, ll_size_type, lll>> {
protected:
Solution solution;
};

TEST_P(Convert1dTo2dArrayTest, CheckSolution) {
const ll input = std::get<0>(GetParam());
const ll_size_type m = std::get<1>(GetParam());
const ll_size_type n = std::get<2>(GetParam());
const lll expected = std::get<3>(GetParam());

auto actual = solution.construct2DArray(input, m, n);

EXPECT_EQ(expected.size(), actual.size());
for (ll_size_type i = 0; i < expected.size(); ++i) {
const auto& row = actual[i];
const auto& expected_row = expected[i];
EXPECT_EQ(expected_row.size(), row.size());
for (ll_size_type j = 0; j < expected_row.size(); ++j) {
EXPECT_EQ(expected_row[j], row[j]);
}
}
}

INSTANTIATE_TEST_SUITE_P(
Convert1dTo2dArrayTests,
Convert1dTo2dArrayTest,
::testing::Values(
std::tuple<ll, ll_size_type, ll_size_type, lll>({1, 2, 3}, 1, 3, {{1, 2, 3}}),
std::tuple<ll, ll_size_type, ll_size_type, lll>({ 1, 2, 3 }, 3, 1, { {1}, {2}, {3} }),
std::tuple<ll, ll_size_type, ll_size_type, lll>({ 1, 2, 3, 4, 5, 6 }, 3, 2, { {1, 2}, {3, 4}, {5, 6} }),
std::tuple<ll, ll_size_type, ll_size_type, lll>({ 1 }, 3, 2, { }),
std::tuple<ll, ll_size_type, ll_size_type, lll>({ 1, 2, 3 }, 1, 2, { })
)
);
5 changes: 3 additions & 2 deletions utils/print_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <ostream>
#include <vector>

std::ostream& operator<<(std::ostream& os, const std::vector<int>& vector)
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector)
{
os << "[";
bool first = true;
for (const int i : vector)
for (const auto& i : vector)
{
if (!first)
os<<",";
Expand Down

0 comments on commit 3ff2f02

Please sign in to comment.