leetcode

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

commit a4090374dcaad3d0c43602f47dadd0399d6a6148
parent d4141c637fa4f99dd0b8487730cf5f52468d2ad6
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 14 Jun 2023 14:15:00 +0200

Random Problem

Diffstat:
AProblems/0532.cpp | 16++++++++++++++++
MREADME.md | 1+
2 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/Problems/0532.cpp b/Problems/0532.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int findPairs(vector<int> &nums, int k) { + int res = 0; + if (k == 0) { + unordered_map<int, int> um; + for (int n : nums) um[n]++; + for (const auto &[n, v] : um) res += v >= 2; + return res; + } else { + unordered_set<int> us(nums.begin(), nums.end()); + for (const auto &n : us) res += us.count(n + k) + us.count(n - k); + return res / 2; + } + } +}; diff --git a/README.md b/README.md @@ -291,6 +291,7 @@ for solving problems. | 0518 | Medium | [Coin Change II](Problems/0518.cpp) | | 0520 | Easy | [Detect Capital](Problems/0520.cpp) | | 0530 | Easy | [Minimum Absolute Difference in BST](Problems/0530.cpp) | +| 0532 | Medium | [K-diff Pairs in an Array](Problems/0532.cpp) | | 0538 | Medium | [Convert BST to Greater Tree](Problems/0538.cpp) | | 0540 | Medium | [Single Element in a Sorted Array](Problems/0540.cpp) | | 0542 | Medium | [01 Matrix](Problems/0542.cpp) |