-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, { }) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters