Skip to content

Commit fd52e46

Browse files
committed
Add solution #1095
1 parent 5d54dbb commit fd52e46

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,133 LeetCode solutions in JavaScript
1+
# 1,134 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -875,6 +875,7 @@
875875
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
876876
1093|[Statistics from a Large Sample](./solutions/1093-statistics-from-a-large-sample.js)|Medium|
877877
1094|[Car Pooling](./solutions/1094-car-pooling.js)|Medium|
878+
1095|[Find in Mountain Array](./solutions/1095-find-in-mountain-array.js)|Hard|
878879
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
879880
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
880881
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* 1095. Find in Mountain Array
3+
* https://leetcode.com/problems/find-in-mountain-array/
4+
* Difficulty: Hard
5+
*
6+
* You may recall that an array arr is a mountain array if and only if:
7+
* - arr.length >= 3
8+
* - There exists some i with 0 < i < arr.length - 1 such that:
9+
* - arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
10+
* - arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
11+
*
12+
* Given a mountain array mountainArr, return the minimum index such that
13+
* mountainArr.get(index) == target. If such an index does not exist, return -1.
14+
*
15+
* You cannot access the mountain array directly. You may only access the array
16+
* using a MountainArray interface:
17+
* - MountainArray.get(k) returns the element of the array at index k (0-indexed).
18+
* - MountainArray.length() returns the length of the array.
19+
*
20+
* Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer.
21+
* Also, any solutions that attempt to circumvent the judge will result in disqualification.
22+
*/
23+
24+
/**
25+
* @param {number} target
26+
* @param {MountainArray} mountainArr
27+
* @return {number}
28+
*/
29+
var findInMountainArray = function(target, mountainArr) {
30+
const length = mountainArr.length();
31+
32+
let left = 0;
33+
let right = length - 1;
34+
while (left < right) {
35+
const mid = Math.floor((left + right) / 2);
36+
if (mountainArr.get(mid) < mountainArr.get(mid + 1)) {
37+
left = mid + 1;
38+
} else {
39+
right = mid;
40+
}
41+
}
42+
const peak = left;
43+
44+
left = 0;
45+
right = peak;
46+
while (left <= right) {
47+
const mid = Math.floor((left + right) / 2);
48+
const value = mountainArr.get(mid);
49+
if (value === target) return mid;
50+
if (value < target) {
51+
left = mid + 1;
52+
} else {
53+
right = mid - 1;
54+
}
55+
}
56+
57+
left = peak;
58+
right = length - 1;
59+
while (left <= right) {
60+
const mid = Math.floor((left + right) / 2);
61+
const value = mountainArr.get(mid);
62+
if (value === target) return mid;
63+
if (value > target) {
64+
left = mid + 1;
65+
} else {
66+
right = mid - 1;
67+
}
68+
}
69+
70+
return -1;
71+
};

0 commit comments

Comments
 (0)