-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add find peak element * impl find peak element
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <algorithm> | ||
|
||
#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<vector<int>, int>( | ||
{{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<int>& 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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters