From 209dcdabf4fbc92b22be4103628c61bd24223508 Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Thu, 7 Mar 2024 18:36:01 +0800 Subject: [PATCH 1/2] add find peak element --- leetcode-cc/FindPeakElement.hpp | 26 ++++++++++++++++++++++++++ runtime-cc/src/registration.cc | 6 ++++++ 2 files changed, 32 insertions(+) create mode 100644 leetcode-cc/FindPeakElement.hpp diff --git a/leetcode-cc/FindPeakElement.hpp b/leetcode-cc/FindPeakElement.hpp new file mode 100644 index 0000000..2e71ccd --- /dev/null +++ b/leetcode-cc/FindPeakElement.hpp @@ -0,0 +1,26 @@ +#include "TestHelper.h" +#include "problem.h" +#include "solution.h" + +using namespace std; + +IMPLEMENT_PROBLEM_CLASS(PFindPeakElement, 162, DIFFI_MEDIUM, TOPIC_ALGORITHMS, + "Find Peak Element", "", {""}); + +class SFindPeakElement : public ISolution { + public: + size_t problemId() const override { return 162; } + string name() const override { + return ("Solution for " + string("Find Peak Element")); + } + string location() const override { return __FILE_NAME__; } + int test() const override { + return testHelper, int>( + {{1, 2, 3, 1}, {1, 2, 1, 3, 5, 6, 4}}, {2, 5}, + [this](auto input) { return this->findPeakElement(input); }); + }; + int benchmark() const override { return 0; } + + private: + int findPeakElement(vector& nums) const {} +}; diff --git a/runtime-cc/src/registration.cc b/runtime-cc/src/registration.cc index 1592fca..910a49d 100644 --- a/runtime-cc/src/registration.cc +++ b/runtime-cc/src/registration.cc @@ -23,6 +23,7 @@ #include "../leetcode-cc/EvaluateReversePolishNotation.hpp" #include "../leetcode-cc/FindMinInRotatedSortedArray.hpp" #include "../leetcode-cc/FindMinInRotatedSortedArrayII.hpp" +#include "../leetcode-cc/FindPeakElement.hpp" #include "../leetcode-cc/FindPlayersWithZeroOrOneLosses.hpp" #include "../leetcode-cc/FlattenBTreeToLinkedList.hpp" #include "../leetcode-cc/FourSum.hpp" @@ -693,5 +694,10 @@ 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 2a235c3a2af6e8329f796b52362248eaac9e3f0d Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Thu, 7 Mar 2024 18:52:17 +0800 Subject: [PATCH 2/2] impl find peak element --- leetcode-cc/FindPeakElement.hpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/leetcode-cc/FindPeakElement.hpp b/leetcode-cc/FindPeakElement.hpp index 2e71ccd..ea638e8 100644 --- a/leetcode-cc/FindPeakElement.hpp +++ b/leetcode-cc/FindPeakElement.hpp @@ -1,3 +1,5 @@ +#include + #include "TestHelper.h" #include "problem.h" #include "solution.h" @@ -16,11 +18,37 @@ class SFindPeakElement : public ISolution { string location() const override { return __FILE_NAME__; } int test() const override { return testHelper, int>( - {{1, 2, 3, 1}, {1, 2, 1, 3, 5, 6, 4}}, {2, 5}, + {{1, 2, 3, 1}, {1, 2, 1, 3, 5, 6, 4}}, {2, 1}, [this](auto input) { return this->findPeakElement(input); }); }; int benchmark() const override { return 0; } private: - int findPeakElement(vector& nums) const {} + int findPeakElement(vector& nums) const { + int n = nums.size(); + if (n == 1) return 0; + if (n == 2) return nums[0] < nums[1] ? 1 : 0; + + if (nums[0] > nums[1]) { + return 0; + } + + int prev = 0; + int current = 1; + int next = 2; + + while (next <= n) { + if (next == n) { + return nums[prev] < nums[current] ? current : prev; + } else if (nums[prev] < nums[current] && nums[next] < nums[current]) { + return current; + } else { + prev++; + current++; + next++; + } + } + + return -1; + } };