generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: solve problem 725. Split Linked List in Parts (#1873)
* test: add test for problem 725. Split Linked List in Parts * feat(problem-725/cpp): solve by counting list size before distributing
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 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,6 @@ | ||
get_dir_name(id) | ||
|
||
add_executable(test-${id} test.cpp) | ||
target_link_libraries(test-${id} PRIVATE Catch2::Catch2WithMain) | ||
|
||
catch_discover_tests(test-${id}) |
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,29 @@ | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
std::vector<ListNode*> splitListToParts(ListNode* head, int k) { | ||
int n{0}; | ||
auto node = head; | ||
while (node != nullptr) { | ||
++n; | ||
node = node->next; | ||
} | ||
|
||
std::vector<ListNode*> output(k); | ||
for (int i{0}; i < k; ++i) { | ||
output[i] = head; | ||
for (int j{n / k + (i < n % k ? 1 : 0)}; j > 1; --j) { | ||
head = head->next; | ||
} | ||
|
||
if (head != nullptr) { | ||
node = head; | ||
head = head->next; | ||
node->next = nullptr; | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
}; |
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,54 @@ | ||
struct ListNode { | ||
int val; | ||
ListNode* next; | ||
ListNode() : val{0}, next{nullptr} {} | ||
ListNode(int x) : val{x}, next{nullptr} {} | ||
ListNode(int x, ListNode* next) : val{x}, next{next} {} | ||
}; | ||
|
||
// clang-format off | ||
#include "solution.cpp" | ||
// clang-format on | ||
|
||
#include <algorithm> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <vector> | ||
|
||
ListNode* to_list(const std::vector<int>& nums) { | ||
if (nums.empty()) return nullptr; | ||
const auto head = new ListNode(nums[0]); | ||
auto node = head; | ||
for (std::size_t i{1}; i < nums.size(); ++i) { | ||
node->next = new ListNode(nums[i]); | ||
node = node->next; | ||
} | ||
return head; | ||
} | ||
|
||
std::vector<std::vector<int>> from_lists(const std::vector<ListNode*>& heads) { | ||
std::vector<std::vector<int>> nums(heads.size()); | ||
for (std::size_t i{0}; i < heads.size(); ++i) { | ||
auto head = heads[i]; | ||
while (head != nullptr) { | ||
nums[i].push_back(head->val); | ||
head = head->next; | ||
} | ||
} | ||
return nums; | ||
} | ||
|
||
TEST_CASE("725. Split Linked List in Parts") { | ||
SECTION("Example 1") { | ||
const auto output = Solution{}.splitListToParts(to_list({1, 2, 3}), 5); | ||
const std::vector<std::vector<int>> expected{{1}, {2}, {3}, {}, {}}; | ||
REQUIRE(from_lists(output) == expected); | ||
} | ||
|
||
SECTION("Example 2") { | ||
const auto output = Solution{}.splitListToParts( | ||
to_list({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), 3); | ||
const std::vector<std::vector<int>> expected{ | ||
{1, 2, 3, 4}, {5, 6, 7}, {8, 9, 10}}; | ||
REQUIRE(from_lists(output) == expected); | ||
} | ||
} |