Skip to content

Commit

Permalink
feat: solve problem 725. Split Linked List in Parts (#1873)
Browse files Browse the repository at this point in the history
* 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
threeal committed Sep 11, 2024
1 parent 34bf99b commit 15cdc5f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [703. Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | Easy | [C++](./old-problems/0703/solution.cpp) |
| [713. Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium | [C](./old-problems/0713/c/solution.c) [C++](./old-problems/0713/solution.cpp) |
| [719. Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/) | Hard | [C](./old-problems/0719/c/solution.c) [C++](./old-problems/0719/solution.cpp) |
| [725. Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | Medium | [C++](./old-problems/0725/solution.cpp) |
| [726. Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | Hard | [C++](./problems/0726/solution.cpp) |
| [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | Medium | [C](./old-problems/0739/c/solution.c) [C++](./old-problems/0739/solution.cpp) |
| [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs) | Easy | [C++](./problems/0746/solution.cpp) |
Expand Down
6 changes: 6 additions & 0 deletions old-problems/0725/CMakeLists.txt
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})
29 changes: 29 additions & 0 deletions old-problems/0725/solution.cpp
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;
}
};
54 changes: 54 additions & 0 deletions old-problems/0725/test.cpp
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);
}
}

0 comments on commit 15cdc5f

Please sign in to comment.