-
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.
- Loading branch information
Showing
5 changed files
with
100 additions
and
11 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
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 @@ | ||
add_catch(test_find_in_mountain_array test.cpp) |
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,63 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <vector> | ||
|
||
class MountainArray { | ||
public: | ||
MountainArray(std::vector<int> arr) : arr_(arr), cnt_(0) {} | ||
|
||
int get(int index) { | ||
++cnt_; | ||
return arr_[index]; | ||
} | ||
|
||
int length() const { return arr_.size(); } | ||
|
||
int count() const { return cnt_; } | ||
|
||
private: | ||
std::vector<int> arr_; | ||
int cnt_; | ||
}; | ||
|
||
class Solution { | ||
public: | ||
static int findInMountainArray(int target, MountainArray &arr) { | ||
const auto peak = peakIndexInMountainArray(arr); | ||
|
||
const auto left = binarySearch(0, peak, target, std::less<int>(), arr); | ||
if (arr.get(left) == target) { | ||
return left; | ||
} | ||
|
||
const auto right = binarySearch(peak + 1, arr.length() - 1, target, | ||
std::greater<int>(), arr); | ||
if (arr.get(right) == target) { | ||
return right; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private: | ||
template <typename Comparator> | ||
static int binarySearch(int left, int right, int target, Comparator comp, | ||
MountainArray &arr) { | ||
while (left < right) { | ||
const auto middle = left + (right - left) / 2; | ||
comp(arr.get(middle), target) ? left = middle + 1 : right = middle; | ||
} | ||
return left; | ||
} | ||
|
||
static int peakIndexInMountainArray(MountainArray &arr) { | ||
auto left = 0, right = arr.length() - 1; | ||
while (left < right) { | ||
const auto middle = left + (right - left) / 2; | ||
arr.get(middle) < arr.get(middle + 1) ? left = middle + 1 | ||
: right = middle; | ||
} | ||
return left; | ||
} | ||
}; |
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,31 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> secret; | ||
int target; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.secret{1, 2, 3, 4, 5, 3, 1}, | ||
.target = 3, | ||
.expected = 2, | ||
}, | ||
{ | ||
.secret{0, 1, 2, 4, 2, 1}, | ||
.target = 3, | ||
.expected = -1, | ||
}, | ||
}; | ||
|
||
for (const auto &[secret, target, expected] : test_cases) { | ||
auto array = MountainArray(secret); | ||
const auto actual = Solution::findInMountainArray(target, array); | ||
REQUIRE(expected == actual); | ||
REQUIRE(array.count() <= 100); | ||
} | ||
} |
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