leetcode

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

commit cbaca96c3832ccca2b0d11cc061a5812fb7fffe2
parent 06f43b0a9aa072a9b8befb085d720499f22d8976
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 19 Jan 2024 21:37:59 +0000

3 Random Problems

Diffstat:
AProblems/1797.cpp | 31+++++++++++++++++++++++++++++++
AProblems/2240.cpp | 9+++++++++
AProblems/2592.cpp | 33+++++++++++++++++++++++++++++++++
MREADME.md | 3+++
4 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/Problems/1797.cpp b/Problems/1797.cpp @@ -0,0 +1,31 @@ +class AuthenticationManager { + const int ttl; + mutable unordered_map<string, int> expire; + + public: + AuthenticationManager(int timeToLive) : ttl(timeToLive) {} + + void generate(const string &tokenId, int currentTime) const { + expire.emplace(tokenId, currentTime + ttl); + } + + void renew(const string &tokenId, int currentTime) const { + auto it = expire.find(tokenId); + if (it == end(expire)) return; + if (it->second <= currentTime) { + expire.erase(it); + return; + } + it->second = currentTime + ttl; + } + + int countUnexpiredTokens(int currentTime) const { + for (auto it = begin(expire); it != end(expire);) { + if (it->second <= currentTime) + it = expire.erase(it); + else + it++; + } + return size(expire); + } +}; diff --git a/Problems/2240.cpp b/Problems/2240.cpp @@ -0,0 +1,9 @@ +class Solution { + public: + long long waysToBuyPensPencils(int total, int cost1, int cost2) const { + long long res = 0; + for (; total >= 0; total -= cost1) + res += 1ll + total / cost2; + return res; + } +}; diff --git a/Problems/2592.cpp b/Problems/2592.cpp @@ -0,0 +1,33 @@ +const auto _ = []() { + ios_base::sync_with_stdio(0); + cout.tie(NULL); + cin.tie(NULL); + return NULL; +}(); + +// O(nlogn) +class Solution { + public: + int maximizeGreatness(vector<int> &nums) const { + sort(rbegin(nums), rend(nums)); + + int res = 0; + for (int i = 0; i < size(nums); i++) { + if (nums[i] < nums[res]) res++; + } + + return res; + } +}; + +// O(n) +class Solution { + public: + int maximizeGreatness(const vector<int> &nums) const { + unordered_map<int, int> count; + int res = 0; + for (const int n : nums) + res = max(res, ++count[n]); + return size(nums) - res; + } +}; diff --git a/README.md b/README.md @@ -856,6 +856,7 @@ for solving problems. | 1791 | Easy | [Find Center of Star Graph](Problems/1791.cpp) | | 1793 | Hard | [Maximum Score of a Good Subarray](Problems/1793.cpp) | | 1795 | Easy | [Rearrange Products Table](Problems/1795.cpp) | +| 1797 | Medium | [Design Authentication Manager](Problems/1797.cpp) | | 1798 | Medium | [Maximum Number of Consecutive Values You Can Make](Problems/1798.cpp) | | 1799 | Medium | [Maximize Score After N Operations](Problems/1799.cpp) | | 1802 | Medium | [Maximum Value at a Given Index in a Bounded Array](Problems/1802.cpp) | @@ -951,6 +952,7 @@ for solving problems. | 2232 | Medium | [Minimize Result by Adding Parentheses to Expression](Problems/2232.cpp) | | 2235 | Easy | [Add Two Integers](Problems/2235.cpp) | | 2236 | Easy | [Root Equals Sum of Children](Problems/2236.cpp) | +| 2240 | Medium | [Number of Ways to Buy Pens and Pencils](Problems/2240.cpp) | | 2243 | Easy | [Calculate Digit Sum of a String](Problems/2243.cpp) | | 2244 | Medium | [Minimum Rounds to Complete All Tasks](Problems/2244.cpp) | | 2246 | Hard | [Longest Path With Different Adjacent Characters](Problems/2246.cpp) | @@ -1027,6 +1029,7 @@ for solving problems. | 2551 | Hard | [Put Marbles in Bags](Problems/2551.cpp) | | 2568 | Medium | [Minimum Impossible OR](Problems/2568.cpp) | | 2579 | Medium | [Count Total Number of Colored Cells](Problems/2579.cpp) | +| 2592 | Medium | [Maximize Greatness of an Array](Problems/2592.cpp) | | 2610 | Medium | [Convert an Array Into a 2D Array With Conditions](Problems/2610.cpp) | | 2616 | Medium | [Minimize the Maximum Difference of Pairs](Problems/2616.cpp) | | 2620 | Easy | [Counter](Problems/2620.js) |