Skip to content

Commit

Permalink
Solve Daily Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Mar 12, 2024
1 parent 4075ae4 commit aa9251b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@
| 1165. | [Single-Row Keyboard 🔒](https://leetcode.com/problems/single-row-keyboard/) | [C++](./solutions/single-row-keyboard/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N+A) / O(A)| <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 1167. | [Minimum Cost to Connect Sticks 🔒](https://leetcode.com/problems/minimum-cost-to-connect-sticks/) | [C++](./solutions/minimum-cost-to-connect-sticks/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O((M+N)log(M+N)) / O(M+N)| <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Minimum Spanning Tree-337f5b?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Linked List-77337f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1175. | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [C++](./solutions/prime-arrangements/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | O(N+A) / O(A)| <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 1167. | [Minimum Cost to Connect Sticks 🔒](https://leetcode.com/problems/minimum-cost-to-connect-sticks/) | [C++](./solutions/minimum-cost-to-connect-sticks/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O((M+N)log(M+N)) / O(M+N)| <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> <img src='https://img.shields.io/badge/Minimum Spanning Tree-337f5b?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Linked List-77337f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 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) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1175. | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements/) | [C++](./solutions/prime-arrangements/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
Expand Down Expand Up @@ -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) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN&nbsp;+&nbsp;NX) / O(N)| <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Memoization-33357f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | [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) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 1392. | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [C++](./solutions/longest-happy-prefix/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Rolling Hash-6b7f33?style=flat-square'/> <img src='https://img.shields.io/badge/String Matching-7f3346?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Function-647f33?style=flat-square'/> | |
| 1393. | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss/) | [SQL](./extra/sql/capital-gainloss/solution.sql), [PY](./extra/pandas/capital-gainloss/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
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)
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
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);
}
}
}

0 comments on commit aa9251b

Please sign in to comment.