leetcode

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

commit 9f2ea6325ffd66b6edc744b0ba5bca6ba905c0f0
parent 3cc200f6afb277734d630bbd8b4b737de68a9e84
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 13 Nov 2024 19:35:54 +0100

Daily Problem

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

diff --git a/Problems/2563.cpp b/Problems/2563.cpp @@ -0,0 +1,19 @@ +class Solution { + static long long countLess(const vector<int> &nums, int target) { + long long res = 0; + + for (int i = 0, j = size(nums) - 1; i < j; i++) { + while (i < j && nums[i] + nums[j] > target) + j--; + res += j - i; + } + + return res; + } + + public: + long long countFairPairs(vector<int> &nums, int lower, int upper) const { + sort(begin(nums), end(nums)); + return countLess(nums, upper) - countLess(nums, lower - 1); + } +}; diff --git a/README.md b/README.md @@ -1330,6 +1330,7 @@ reference and a base for solving problems. | 2551 | Hard | [Put Marbles in Bags](Problems/2551.cpp) | | 2554 | Medium | [Maximum Number of Integers to Choose From a Range I](Problems/2554.cpp) | | 2559 | Medium | [Count Vowel Strings in Ranges](Problems/2559.cpp) | +| 2563 | Medium | [Count the Number of Fair Pairs](Problems/2563.cpp) | | 2568 | Medium | [Minimum Impossible OR](Problems/2568.cpp) | | 2571 | Medium | [Minimum Operations to Reduce an Integer to 0](Problems/2571.cpp) | | 2579 | Medium | [Count Total Number of Colored Cells](Problems/2579.cpp) |