leetcode

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

commit bd34a359e0c93902880af95ca88559207f346127
parent cdfcf665d196a8a5dc82ff0f276b62d8372bae0a
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 14 Mar 2023 13:17:37 +0100

Random Problem

Diffstat:
AProblems/0219.cpp | 19+++++++++++++++++++
MREADME.md | 1+
2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/Problems/0219.cpp b/Problems/0219.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool containsNearbyDuplicate(vector<int> &nums, int k) { + unordered_set<int> us; + int n = nums.size(), i; + for (i = 0; i <= k && i < n; i++) { + if (us.count(nums[i])) return true; + us.insert(nums[i]); + } + + for (; i < n; i++) { + us.erase(nums[i - k - 1]); + if (us.count(nums[i])) return true; + us.insert(nums[i]); + } + + return false; + } +}; diff --git a/README.md b/README.md @@ -173,6 +173,7 @@ for solving problems. | 0213 | Medium | [House Robber II](Problems/0213.cpp) | | 0215 | Medium | [Kth Largest Element in an Array](Problems/0215.cpp) | | 0217 | Easy | [Contains Duplicate](Problems/0217.cpp) | +| 0219 | Easy | [Contains Duplicate II](Problems/0219.cpp) | | 0221 | Medium | [Maximal Square](Problems/0221.cpp) | | 0222 | Medium | [Count Complete Tree Nodes](Problems/0222.cpp) | | 0223 | Medium | [Rectangle Area](Problems/0223.cpp) |