From f4fd631b8a83422de41404a195a0e8929c15efc4 Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Thu, 7 Mar 2024 10:19:56 +0800 Subject: [PATCH 1/2] add find min in rotated sorted array --- leetcode-cc/FindMinInRotatedSortedArray.hpp | 30 +++++++++++++++++++++ runtime-cc/src/registration.cc | 8 ++++++ 2 files changed, 38 insertions(+) create mode 100644 leetcode-cc/FindMinInRotatedSortedArray.hpp diff --git a/leetcode-cc/FindMinInRotatedSortedArray.hpp b/leetcode-cc/FindMinInRotatedSortedArray.hpp new file mode 100644 index 0000000..6a9c859 --- /dev/null +++ b/leetcode-cc/FindMinInRotatedSortedArray.hpp @@ -0,0 +1,30 @@ +#include "TestHelper.h" +#include "problem.h" +#include "solution.h" + +using namespace std; + +IMPLEMENT_PROBLEM_CLASS(PFindMinInRotatedSortedArray, 153, DIFFI_MEDIUM, + TOPIC_ALGORITHMS, + "Find Minimum in Rotated Sorted Array", + "Given the sorted rotated array nums of unique " + "elements, return the minimum element of this array.", + {""}); + +class SFindMinInRotatedSortedArray : public ISolution { + public: + size_t problemId() const override { return 153; } + string name() const override { + return ("Solution for " + string("Find Minimum in Rotated Sorted Array")); + } + string location() const override { return __FILE_NAME__; } + int test() const override { + return testHelper, int>( + {{3, 4, 5, 1, 2}, {4, 5, 6, 7, 0, 1, 2}, {11, 13, 15, 17}}, {1, 0, 11}, + [this](auto input) { return this->findMin(input); }); + }; + int benchmark() const override { return 0; } + + private: + int findMin(vector& nums) const {} +}; diff --git a/runtime-cc/src/registration.cc b/runtime-cc/src/registration.cc index b775ecf..73f2d54 100644 --- a/runtime-cc/src/registration.cc +++ b/runtime-cc/src/registration.cc @@ -21,6 +21,7 @@ #include "../leetcode-cc/DistinctSubsequences.hpp" #include "../leetcode-cc/DivideTwoIntegers.hpp" #include "../leetcode-cc/EvaluateReversePolishNotation.hpp" +#include "../leetcode-cc/FindMinInRotatedSortedArray.hpp" #include "../leetcode-cc/FindPlayersWithZeroOrOneLosses.hpp" #include "../leetcode-cc/FlattenBTreeToLinkedList.hpp" #include "../leetcode-cc/FourSum.hpp" @@ -665,5 +666,12 @@ const int registerAll(std::shared_ptr handle) { handle->registerSolution( []() -> ArcSolution { return std::make_shared(); }); + handle->registerProblem([]() -> ArcProblem { + return std::make_shared(); + }); + handle->registerSolution([]() -> ArcSolution { + return std::make_shared(); + }); + return 0; } From 8b19eb8a87ef52d06ca76dfa0ade07e09aa6192f Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Thu, 7 Mar 2024 10:38:55 +0800 Subject: [PATCH 2/2] impl find min in rotated sorted array --- leetcode-cc/FindMinInRotatedSortedArray.hpp | 32 ++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/leetcode-cc/FindMinInRotatedSortedArray.hpp b/leetcode-cc/FindMinInRotatedSortedArray.hpp index 6a9c859..b2dc657 100644 --- a/leetcode-cc/FindMinInRotatedSortedArray.hpp +++ b/leetcode-cc/FindMinInRotatedSortedArray.hpp @@ -1,3 +1,5 @@ +#include + #include "TestHelper.h" #include "problem.h" #include "solution.h" @@ -9,7 +11,7 @@ IMPLEMENT_PROBLEM_CLASS(PFindMinInRotatedSortedArray, 153, DIFFI_MEDIUM, "Find Minimum in Rotated Sorted Array", "Given the sorted rotated array nums of unique " "elements, return the minimum element of this array.", - {""}); + {"Array"}); class SFindMinInRotatedSortedArray : public ISolution { public: @@ -20,11 +22,33 @@ class SFindMinInRotatedSortedArray : public ISolution { string location() const override { return __FILE_NAME__; } int test() const override { return testHelper, int>( - {{3, 4, 5, 1, 2}, {4, 5, 6, 7, 0, 1, 2}, {11, 13, 15, 17}}, {1, 0, 11}, - [this](auto input) { return this->findMin(input); }); + {{3, 4, 5, 1, 2}, {4, 5, 6, 7, 0, 1, 2}, {11, 13, 15, 17}, {2, 1}}, + {1, 0, 11, 1}, [this](auto input) { return this->findMin(input); }); }; int benchmark() const override { return 0; } private: - int findMin(vector& nums) const {} + int findMin(vector& nums) const { + int n = nums.size(); + int start = 0; + int end = n - 1; + + int res = INT_MAX; + + while (start <= end) { + int middle = (start + end) / 2; + + if (nums[start] <= nums[middle]) { + // start..middle is sorted + res = min(res, nums[start]); + start = middle + 1; + } else { + // middle..end is sorted + res = min(res, nums[middle]); + end = middle - 1; + } + } + + return res; + } };