Skip to content

Commit

Permalink
Create 1095_Find_in_Mountain_Array.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuzaima authored Oct 12, 2023
1 parent ca24ef4 commit dffd38d
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions 1095_Find_in_Mountain_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// id: 1095
// Name: Find in Mountain Array
// link: https://leetcode.com/problems/find-in-mountain-array/
// Difficulty: Hard

/**
* // 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 {

private boolean isAscending = true;

private boolean shouldGoRight(int current, int target) {
if (isAscending) {
return current < target;
} else {
return current > target;
}
}


public int findInMountainArray(int target, MountainArray mountainArr) {
int peakIndex = 0;
int left = 0;
int right = mountainArr.length() - 1;

while (left < right) {
int mid = ( left + right ) / 2;

int current = mountainArr.get(mid);
int leftOfMid = mountainArr.get(mid-1);
int rightOfMid = mountainArr.get(mid+1);


// check if at left peak
if (current > leftOfMid && current > rightOfMid) {
peakIndex = mid;
break;
} else if (current > leftOfMid) {
// peak is at right
left = mid ;
} else if (current > rightOfMid) {
right = mid ;
}
}

int result = binarySearch(target, mountainArr, 0, peakIndex);

isAscending = false;

if (result == -1) {
result = binarySearch(target, mountainArr, peakIndex, mountainArr.length()-1);
}

return result;
}


// both start, and end, inclusive
private int binarySearch(int target, MountainArray mountainArr, int start, int end) {
while (start <= end) {
int mid = (start + end) / 2;
int current = mountainArr.get(mid);

if (current == target) {
return mid;
} else if (shouldGoRight(current, target)) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
}

0 comments on commit dffd38d

Please sign in to comment.