In this approach, the task is broken into three main steps:
arr[i] > arr[i+1]
and arr[i] > arr[i-1]
.Time Complexity: O(log n)
for peak finding and 2 * O(log n)
for the two binary searches.
Total: O(log n)
.
Space Complexity: O(1)
because we use a constant amount of space.
1public interface MountainArray {
2 int Get(int index);
3 int Length();
4}
5
6public class Solution {
7 private int FindPeakIndex(MountainArray mountainArr) {
8 int left = 0;
9 int right = mountainArr.Length() - 1;
10 while (left < right) {
11 int mid = left + (right - left) / 2;
12 if (mountainArr.Get(mid) < mountainArr.Get(mid + 1)) {
13 left = mid + 1;
14 } else {
15 right = mid;
16 }
17 }
18 return left;
19 }
20
21 private int BinarySearch(MountainArray mountainArr, int start, int end, int target, bool isAscending) {
22 while (start <= end) {
23 int mid = start + (end - start) / 2;
24 int value = mountainArr.Get(mid);
25 if (value == target) {
26 return mid;
27 }
28 if (isAscending) {
29 if (value < target) {
30 start = mid + 1;
31 } else {
32 end = mid - 1;
33 }
34 } else {
35 if (value > target) {
36 start = mid + 1;
37 } else {
38 end = mid - 1;
39 }
40 }
41 }
42 return -1;
43 }
44
45 public int FindInMountainArray(int target, MountainArray mountainArr) {
46 int peak = FindPeakIndex(mountainArr);
47 int index = BinarySearch(mountainArr, 0, peak, target, true);
48 if (index != -1) {
49 return index;
50 }
51 return BinarySearch(mountainArr, peak + 1, mountainArr.Length() - 1, target, false);
52 }
53}
The C# solution also leverages binary search to efficiently find the peak and then searches on both the ascending and descending segments of the mountain array. This strategy ensures that the solution remains optimal with a time complexity of O(log n)
.
This approach involves directly accessing each element linearly until the condition is satisfied (even though this is not allowed by the problem constraints). It is less optimal and efficient compared to the above implementations, requiring traversal of the entire array.
Time Complexity: O(n)
as each element could potentially be checked once.
Space Complexity: O(1)
as no extra space is used except for variables.
1#include <stdio.h>
2
3// Forward declaration of the MountainArray API.
4// You don't need to implement this interface, it is provided by the problem constraints.
5struct MountainArray {
6 int (*get)(struct MountainArray*, int index);
7 int (*length)(struct MountainArray*);
8};
9
10int findInMountainArray(int target, struct MountainArray* mountainArr) {
11 int len = mountainArr->length(mountainArr);
12 int index = -1;
13 for (int i = 0; i < len; ++i) {
14 if (mountainArr->get(mountainArr, i) == target) {
15 return i;
16 }
17 }
18 return index;
19}
The brute-force approach directly scans through the mountain array from the start to the finish, checking each element against the target. If an element equals the target, it returns the index. Otherwise, it returns -1
. This basic method is straightforward but inefficient due to its time complexity.