-
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
6 changed files
with
101 additions
and
1 deletion.
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
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
1 change: 1 addition & 0 deletions
1
solutions/remove-zero-sum-consecutive-nodes-from-linked-list/CMakeLists.txt
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 @@ | ||
add_catch(test_remove_zero_sum_consecutive_nodes_from_linked_list test.cpp) |
56 changes: 56 additions & 0 deletions
56
solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp
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,56 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
|
||
#include <list_node.h> | ||
|
||
namespace ps { | ||
|
||
// Time: O(N^2) | ||
// Space: O(1) | ||
class Solution { | ||
public: | ||
static ListNode *removeZeroSumSublists(ListNode *head) { | ||
ListNode dummy(0, head); | ||
for (auto *start = &dummy; start; start = start->next) { | ||
int sum = 0; | ||
for (auto *end = start->next; end; end = end->next) { | ||
sum += end->val; | ||
if (!sum) { | ||
start->next = end->next; | ||
} | ||
} | ||
} | ||
return dummy.next; | ||
} | ||
}; | ||
|
||
} // namespace ps | ||
|
||
namespace ht { | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
class Solution { | ||
public: | ||
static ListNode *removeZeroSumSublists(ListNode *head) { | ||
ListNode dummy(0, head); | ||
std::unordered_map<int, ListNode *> map; | ||
int sum = 0; | ||
for (auto *curr = &dummy; curr; curr = curr->next) { | ||
sum += curr->val; | ||
if (map.contains(sum)) { | ||
auto *node = std::exchange(map[sum]->next, curr->next); | ||
for (auto s = sum + node->val; s != sum; s += node->val) { | ||
map.erase(s); | ||
node = node->next; | ||
} | ||
} else { | ||
map[sum] = curr; | ||
} | ||
} | ||
return dummy.next; | ||
} | ||
}; | ||
|
||
} // namespace ht |
41 changes: 41 additions & 0 deletions
41
solutions/remove-zero-sum-consecutive-nodes-from-linked-list/test.cpp
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,41 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
#include <list_node.h> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
List head; | ||
List expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.head = {1, 2, -3, 3, 1}, | ||
.expected = {3, 1}, | ||
}, | ||
{ | ||
.head = {1, 2, 3, -3, 4}, | ||
.expected = {1, 2, 4}, | ||
}, | ||
{ | ||
.head = {1, 2, 3, -3, -2}, | ||
.expected = {1}, | ||
}, | ||
}; | ||
|
||
SECTION("Prefix Sum") { | ||
for (const auto &[head, expected] : test_cases) { | ||
const List actual = ps::Solution::removeZeroSumSublists(Copy(head)); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
|
||
SECTION("Hash Table") { | ||
for (const auto &[head, expected] : test_cases) { | ||
const List actual = ht::Solution::removeZeroSumSublists(Copy(head)); | ||
REQUIRE(expected == actual); | ||
} | ||
} | ||
} |