From aa9251be0c137a61db45c8f1d9cf9d3e3231e07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Tue, 12 Mar 2024 12:17:08 +0300 Subject: [PATCH] Solve Daily Problem --- CMakeLists.txt | 1 + PROBLEM_LIST.md | 1 + README.md | 2 +- .../CMakeLists.txt | 1 + .../solution.hpp | 56 +++++++++++++++++++ .../test.cpp | 41 ++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 solutions/remove-zero-sum-consecutive-nodes-from-linked-list/CMakeLists.txt create mode 100644 solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp create mode 100644 solutions/remove-zero-sum-consecutive-nodes-from-linked-list/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4743e04b..ff0d3bcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1301,6 +1301,7 @@ add_task(remove-outermost-parentheses) add_task(remove-palindromic-subsequences) add_task(remove-trailing-zeros-from-a-string) add_task(remove-vowels-from-a-string) +add_task(remove-zero-sum-consecutive-nodes-from-linked-list) add_task(removing-stars-from-a-string) add_task(reorder-list) add_task(reorder-routes-to-make-all-paths-lead-to-the-city-zero) diff --git a/PROBLEM_LIST.md b/PROBLEM_LIST.md index 12d969dc..ba1d8f81 100644 --- a/PROBLEM_LIST.md +++ b/PROBLEM_LIST.md @@ -850,6 +850,7 @@ | 1165. | [Single-Row Keyboard 🔒](https://leetcode.com/problems/single-row-keyboard/) | [C++](./solutions/single-row-keyboard/solution.hpp) | | O(N+A) / O(A)| | | | 1167. | [Minimum Cost to Connect Sticks 🔒](https://leetcode.com/problems/minimum-cost-to-connect-sticks/) | [C++](./solutions/minimum-cost-to-connect-sticks/solution.hpp) | | O(NlogN) / O(N)| | | | 1168. | [Optimize Water Distribution in a Village 🔒](https://leetcode.com/problems/optimize-water-distribution-in-a-village/) | [C++](./solutions/optimize-water-distribution-in-a-village/solution.hpp) | | O((M+N)log(M+N)) / O(M+N)| | | +| 1171. | [Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [C++](./solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp) | | | | | | 1173. | [Immediate Food Delivery I 🔒](https://leetcode.com/problems/immediate-food-delivery-i/) | [SQL](./extra/sql/immediate-food-delivery-i/solution.sql), [PY](./extra/pandas/immediate-food-delivery-i/solution.py) | | | | | | 1174. | [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/) | [SQL](./extra/sql/immediate-food-delivery-ii/solution.sql), [PY](./extra/pandas/immediate-food-delivery-ii/solution.py) | | | | | | 1175. | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [C++](./solutions/prime-arrangements/solution.hpp) | | | | | diff --git a/README.md b/README.md index 0a18c0f3..b08a948b 100644 --- a/README.md +++ b/README.md @@ -944,6 +944,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th | 1165. | [Single-Row Keyboard 🔒](https://leetcode.com/problems/single-row-keyboard/) | [C++](./solutions/single-row-keyboard/solution.hpp) | | O(N+A) / O(A)| | | | 1167. | [Minimum Cost to Connect Sticks 🔒](https://leetcode.com/problems/minimum-cost-to-connect-sticks/) | [C++](./solutions/minimum-cost-to-connect-sticks/solution.hpp) | | O(NlogN) / O(N)| | | | 1168. | [Optimize Water Distribution in a Village 🔒](https://leetcode.com/problems/optimize-water-distribution-in-a-village/) | [C++](./solutions/optimize-water-distribution-in-a-village/solution.hpp) | | O((M+N)log(M+N)) / O(M+N)| | | +| 1171. | [Remove Zero Sum Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [C++](./solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp) | | | | | | 1173. | [Immediate Food Delivery I 🔒](https://leetcode.com/problems/immediate-food-delivery-i/) | [SQL](./extra/sql/immediate-food-delivery-i/solution.sql), [PY](./extra/pandas/immediate-food-delivery-i/solution.py) | | | | | | 1174. | [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/) | [SQL](./extra/sql/immediate-food-delivery-ii/solution.sql), [PY](./extra/pandas/immediate-food-delivery-ii/solution.py) | | | | | | 1175. | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [C++](./solutions/prime-arrangements/solution.hpp) | | | | | @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th | 1387. | [Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/) | [C++](./solutions/sort-integers-by-the-power-value/solution.hpp) | | O(NlogN + NX) / O(N)| | [Collatz conjecture](https://w.wiki/329) | | 1389. | [Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/) | [C++](./solutions/create-target-array-in-the-given-order/solution.hpp) | | | | | | 1392. | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [C++](./solutions/longest-happy-prefix/solution.hpp) | | O(N) / O(1)| | | -| 1393. | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss/) | [SQL](./extra/sql/capital-gainloss/solution.sql), [PY](./extra/pandas/capital-gainloss/solution.py) | | | | | diff --git a/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/CMakeLists.txt b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/CMakeLists.txt new file mode 100644 index 00000000..31a7492f --- /dev/null +++ b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_remove_zero_sum_consecutive_nodes_from_linked_list test.cpp) diff --git a/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp new file mode 100644 index 00000000..2a728a2a --- /dev/null +++ b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/solution.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include + +#include + +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 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 diff --git a/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/test.cpp b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/test.cpp new file mode 100644 index 00000000..88d3d947 --- /dev/null +++ b/solutions/remove-zero-sum-consecutive-nodes-from-linked-list/test.cpp @@ -0,0 +1,41 @@ +#include + +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + List head; + List expected; + }; + + std::vector 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); + } + } +}