leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 75204a6fc4bacaceadaa5a523db6ef8162e5b77d
parent ce808bbaf2422358a38f0849274346a4071cb777
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 12 Mar 2024 15:20:50 +0000

1 Random Problem

Diffstat:
AProblems/1171.cpp | 28++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/Problems/1171.cpp b/Problems/1171.cpp @@ -0,0 +1,28 @@ +class Solution { + public: + ListNode *removeZeroSumSublists(ListNode *head) const { + unordered_map<int, ListNode *> um; + ListNode dummy(0, head); + int crnt = 0; + + for (ListNode *c = &dummy; c; c = c->next) { + crnt += c->val; + if (um.count(crnt)) { + ListNode *p = um[crnt]; + c = p->next; + + int v = crnt + c->val; + while (v != crnt) { + um.erase(v); + c = c->next; + v += c->val; + } + + p->next = c->next; + } else + um[crnt] = c; + } + + return dummy.next; + } +}; diff --git a/README.md b/README.md @@ -649,6 +649,7 @@ for solving problems. | 1162 | Medium | [As Far from Land as Possible](Problems/1162.cpp) | | 1164 | Medium | [Product Price at a Given Date](Problems/1164.cpp) | | 1170 | Medium | [Compare Strings by Frequency of the Smallest Character](Problems/1170.cpp) | +| 1171 | Medium | [Remove Zero Sum Consecutive Nodes from Linked List](Problems/1171.cpp) | | 1174 | Medium | [Immediate Food Delivery II](Problems/1174.cpp) | | 1179 | Easy | [Reformat Department Table](Problems/1179.cpp) | | 1187 | Hard | [Make Array Strictly Increasing](Problems/1187.cpp) |