leetcode

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

commit 25b4aa57228ad8118fb819c1c46f68a957817b6f
parent 1d5b88ed1ba85ff6c71073dd5fbdf7b4e6896b68
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 24 Oct 2023 21:44:05 +0000

2 Random Problems

Diffstat:
AProblems/0553.cpp | 13+++++++++++++
AProblems/2452.cpp | 23+++++++++++++++++++++++
MREADME.md | 2++
3 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/Problems/0553.cpp b/Problems/0553.cpp @@ -0,0 +1,13 @@ +class Solution { + public: + string optimalDivision(const vector<int> &nums) { + const int n = nums.size(); + if (n == 1) return to_string(nums[0]); + if (n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]); + string res = to_string(nums[0]) + "/(" + to_string(nums[1]); + for (int i = 2; i < n; i++) + res += "/" + to_string(nums[i]); + res += ')'; + return res; + } +}; diff --git a/Problems/2452.cpp b/Problems/2452.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + vector<string> twoEditWords(const vector<string> &queries, const vector<string> &dictionary) { + const int n = queries.front().size(); + + vector<string> res; + if (n <= 2) return queries; + for (const string &query : queries) { + for (const string &dict : dictionary) { + int i = 0, j = 0, diff = 0; + while (i < n) { + if (query[i++] == dict[j++]) continue; + if (++diff > 2) goto next; + } + res.push_back(query); + break; + next:; + } + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -334,6 +334,7 @@ for solving problems. | 0542 | Medium | [01 Matrix](Problems/0542.cpp) | | 0543 | Easy | [Diameter of Binary Tree](Problems/0543.cpp) | | 0547 | Medium | [Number of Provinces](Problems/0547.cpp) | +| 0553 | Medium | [Optimal Division](Problems/0553.cpp) | | 0556 | Medium | [Next Greater Element III](Problems/0556.cpp) | | 0557 | Easy | [Reverse Words in a String III](Problems/0557.cpp) | | 0559 | Easy | [Maximum Depth of N-ary Tree](Problems/0559.cpp) | @@ -829,6 +830,7 @@ for solving problems. | 2442 | Medium | [Count Number of Distinct Integers After Reverse Operations](Problems/2442.cpp) | | 2444 | Hard | [Count Subarrays With Fixed Bounds](Problems/2444.cpp) | | 2448 | Hard | [Minimum Cost to Make Array Equal](Problems/2448.cpp) | +| 2452 | Medium | [Words Within Two Edits of Dictionary](Problems/2452.cpp) | | 2461 | Medium | [Maximum Sum of Distinct Subarrays With Length K](Problems/2461.cpp) | | 2462 | Medium | [Total Cost to Hire K Workers](Problems/2462.cpp) | | 2465 | Easy | [Number of Distinct Averages](Problems/2465.cpp) |