Skip to content

Create: 1095-find-in-mountain-array.java #3061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions java/1095-find-in-mountain-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* // This is MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* interface MountainArray {
* public int get(int index) {}
* public int length() {}
* }
*/

class Solution {
public int findInMountainArray(int target, MountainArray mountainArr) {
int maxIndex = findMax(mountainArr);
int leftResult = binarySearchLeft(mountainArr, target, maxIndex);
int rightResult = binarySearchRight(mountainArr, target, maxIndex);

return (leftResult == -1 && rightResult == -1) ? -1 : leftResult == -1 ? rightResult : leftResult;
}

public int findMax(MountainArray array) {
int left = 0;
int right = array.length() - 1;

while (left<=right) {
int mid = left + (right - left)/2;
if (array.get(mid) < array.get(mid + 1)) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return left;
}

public int binarySearchLeft(MountainArray array, int target, int right) {
int left = 0;

while (left<=right) {
int mid = left + (right - left)/2;
int midValue = array.get(mid);
if (midValue < target) {
left = mid + 1;
} else if (midValue > target) {
right = mid - 1;
} else {
return mid;
}
}
return -1;
}

public int binarySearchRight(MountainArray array, int target, int left) {
int right = array.length() - 1;

while (left <= right) {
int mid = left + (right - left)/2;
int midValue = array.get(mid);

if (midValue < target) {
right = mid - 1;
} else if (midValue > target) {
left = mid + 1;
} else {
return mid;
}
}
return -1;
}
}