Skip to content

Commit b66dabf

Browse files
committed
+ problem 1095
1 parent ee15fd6 commit b66dabf

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 1095. Find in Mountain Array
2+
*(This problem is an **interactive problem**.)*
3+
4+
You may recall that an array `arr` is a **mountain array** if and only if:
5+
* `arr.length >= 3`
6+
* There exists some `i` with `0 < i < arr.length - 1` such that:
7+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
8+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
9+
10+
Given a mountain array `mountainArr`, return the **minimum** `index` such that `mountainArr.get(index) == target`. If such an `index` does not exist, return `-1`.
11+
12+
**You cannot access the mountain array directly**. You may only access the array using a `MountainArray` interface:
13+
* `MountainArray.get(k)` returns the element of the array at index `k` (0-indexed).
14+
* `MountainArray.length()` returns the length of the array.
15+
16+
Submissions making more than `100` calls to `MountainArray.get` will be judged *Wrong Answer*. Also, any solutions that attempt to circumvent the judge will result in disqualification.
17+
18+
#### Example 1:
19+
<pre>
20+
<strong>Input:</strong> mountainArr = [1,2,3,4,5,3,1], target = 3
21+
<strong>Output:</strong> 2
22+
<strong>Explanation:</strong> 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
23+
</pre>
24+
25+
#### Example 2:
26+
<pre>
27+
<strong>Input:</strong> mountainArr = [0,1,2,4,2,1], target = 3
28+
<strong>Output:</strong> -1
29+
<strong>Explanation:</strong> 3 does not exist in the array, so we return -1.
30+
</pre>
31+
32+
#### Constraints:
33+
* <code>3 <= mountainArr.length() <= 10<sup>4</sup></code>
34+
* <code>0 <= target <= 10<sup>9</sup></code>
35+
* <code>0 <= mountainArr.get(index) <= 10<sup>9</sup></code>
36+
37+
## Solutions (Python)
38+
39+
### 1. Solution
40+
```Python
41+
# """
42+
# This is MountainArray's API interface.
43+
# You should not implement it, or speculate about its implementation
44+
# """
45+
# class MountainArray:
46+
# def get(self, index: int) -> int:
47+
# def length(self) -> int:
48+
49+
class Solution:
50+
def findInMountainArray(self, target: int, mountainArr: 'MountainArray') -> int:
51+
n = mountainArr.length()
52+
i = bisect.bisect(range(n - 1), False, key=lambda j: mountainArr.get(
53+
j) > mountainArr.get(j + 1))
54+
55+
index = bisect.bisect_left(range(n), target, hi=i, key=mountainArr.get)
56+
if mountainArr.get(index) == target:
57+
return index
58+
index = bisect.bisect_left(
59+
range(n - 1), -target, lo=i, key=lambda j: -mountainArr.get(j))
60+
if mountainArr.get(index) == target:
61+
return index
62+
63+
return -1
64+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 1095. 山脉数组中查找目标值
2+
(这是一个 **交互式问题**
3+
4+
你可以将一个数组 `arr` 称为 **山脉数组** 当且仅当:
5+
* `arr.length >= 3`
6+
* 存在一些 `0 < i < arr.length - 1``i` 使得:
7+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
8+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
9+
10+
给定一个山脉数组 `mountainArr` ,返回 **最小**`index` 使得 `mountainArr.get(index) == target`。如果不存在这样的 `index`,返回 `-1`
11+
12+
**你无法直接访问山脉数组**。你只能使用 `MountainArray` 接口来访问数组:
13+
* `MountainArray.get(k)` 返回数组中下标为 `k` 的元素(从 0 开始)。
14+
* `MountainArray.length()` 返回数组的长度。
15+
16+
调用 `MountainArray.get` 超过 `100` 次的提交会被判定为错误答案。此外,任何试图绕过在线评测的解决方案都将导致取消资格。
17+
18+
#### 示例 1:
19+
<pre>
20+
<strong>输入:</strong> mountainArr = [1,2,3,4,5,3,1], target = 3
21+
<strong>输出:</strong> 2
22+
<strong>解释:</strong> 3 在数组中出现了两次,下标分别为 2 和 5,我们返回最小的下标 2。
23+
</pre>
24+
25+
#### 示例 2:
26+
<pre>
27+
<strong>输入:</strong> mountainArr = [0,1,2,4,2,1], target = 3
28+
<strong>输出:</strong> -1
29+
<strong>解释:</strong> 3 在数组中没有出现,返回 -1。
30+
</pre>
31+
32+
#### 提示:
33+
* <code>3 <= mountainArr.length() <= 10<sup>4</sup></code>
34+
* <code>0 <= target <= 10<sup>9</sup></code>
35+
* <code>0 <= mountainArr.get(index) <= 10<sup>9</sup></code>
36+
37+
## 题解 (Python)
38+
39+
### 1. 题解
40+
```Python
41+
# """
42+
# This is MountainArray's API interface.
43+
# You should not implement it, or speculate about its implementation
44+
# """
45+
# class MountainArray:
46+
# def get(self, index: int) -> int:
47+
# def length(self) -> int:
48+
49+
class Solution:
50+
def findInMountainArray(self, target: int, mountainArr: 'MountainArray') -> int:
51+
n = mountainArr.length()
52+
i = bisect.bisect(range(n - 1), False, key=lambda j: mountainArr.get(
53+
j) > mountainArr.get(j + 1))
54+
55+
index = bisect.bisect_left(range(n), target, hi=i, key=mountainArr.get)
56+
if mountainArr.get(index) == target:
57+
return index
58+
index = bisect.bisect_left(
59+
range(n - 1), -target, lo=i, key=lambda j: -mountainArr.get(j))
60+
if mountainArr.get(index) == target:
61+
return index
62+
63+
return -1
64+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# """
2+
# This is MountainArray's API interface.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
# class MountainArray:
6+
# def get(self, index: int) -> int:
7+
# def length(self) -> int:
8+
9+
class Solution:
10+
def findInMountainArray(self, target: int, mountainArr: 'MountainArray') -> int:
11+
n = mountainArr.length()
12+
i = bisect.bisect(range(n - 1), False, key=lambda j: mountainArr.get(
13+
j) > mountainArr.get(j + 1))
14+
15+
index = bisect.bisect_left(range(n), target, hi=i, key=mountainArr.get)
16+
if mountainArr.get(index) == target:
17+
return index
18+
index = bisect.bisect_left(
19+
range(n - 1), -target, lo=i, key=lambda j: -mountainArr.get(j))
20+
if mountainArr.get(index) == target:
21+
return index
22+
23+
return -1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
[1091][1091l]|[Shortest Path in Binary Matrix][1091] |![rs]
766766
[1093][1093l]|[Statistics from a Large Sample][1093] |![rs]
767767
[1094][1094l]|[Car Pooling][1094] |![rs]
768+
[1095][1095l]|[Find in Mountain Array][1095] |![py]
768769
[1103][1103l]|[Distribute Candies to People][1103] |![py]
769770
[1104][1104l]|[Path In Zigzag Labelled Binary Tree][1104] |![rs]
770771
[1105][1105l]|[Filling Bookcase Shelves][1105] |![rs]
@@ -2449,6 +2450,7 @@
24492450
[1091]:Problemset/1091-Shortest%20Path%20in%20Binary%20Matrix/README.md#1091-shortest-path-in-binary-matrix
24502451
[1093]:Problemset/1093-Statistics%20from%20a%20Large%20Sample/README.md#1093-statistics-from-a-large-sample
24512452
[1094]:Problemset/1094-Car%20Pooling/README.md#1094-car-pooling
2453+
[1095]:Problemset/1095-Find%20in%20Mountain%20Array/README.md#1095-find-in-mountain-array
24522454
[1103]:Problemset/1103-Distribute%20Candies%20to%20People/README.md#1103-distribute-candies-to-people
24532455
[1104]:Problemset/1104-Path%20In%20Zigzag%20Labelled%20Binary%20Tree/README.md#1104-path-in-zigzag-labelled-binary-tree
24542456
[1105]:Problemset/1105-Filling%20Bookcase%20Shelves/README.md#1105-filling-bookcase-shelves
@@ -4127,6 +4129,7 @@
41274129
[1091l]:https://leetcode.com/problems/shortest-path-in-binary-matrix/
41284130
[1093l]:https://leetcode.com/problems/statistics-from-a-large-sample/
41294131
[1094l]:https://leetcode.com/problems/car-pooling/
4132+
[1095l]:https://leetcode.com/problems/find-in-mountain-array/
41304133
[1103l]:https://leetcode.com/problems/distribute-candies-to-people/
41314134
[1104l]:https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/
41324135
[1105l]:https://leetcode.com/problems/filling-bookcase-shelves/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
[1091][1091l]|[二进制矩阵中的最短路径][1091] |![rs]
766766
[1093][1093l]|[大样本统计][1093] |![rs]
767767
[1094][1094l]|[拼车][1094] |![rs]
768+
[1095][1095l]|[山脉数组中查找目标值][1095] |![py]
768769
[1103][1103l]|[分糖果 II][1103] |![py]
769770
[1104][1104l]|[二叉树寻路][1104] |![rs]
770771
[1105][1105l]|[填充书架][1105] |![rs]
@@ -2449,6 +2450,7 @@
24492450
[1091]:Problemset/1091-Shortest%20Path%20in%20Binary%20Matrix/README_CN.md#1091-二进制矩阵中的最短路径
24502451
[1093]:Problemset/1093-Statistics%20from%20a%20Large%20Sample/README_CN.md#1093-大样本统计
24512452
[1094]:Problemset/1094-Car%20Pooling/README_CN.md#1094-拼车
2453+
[1095]:Problemset/1095-Find%20in%20Mountain%20Array/README_CN.md#1095-山脉数组中查找目标值
24522454
[1103]:Problemset/1103-Distribute%20Candies%20to%20People/README_CN.md#1103-分糖果-ii
24532455
[1104]:Problemset/1104-Path%20In%20Zigzag%20Labelled%20Binary%20Tree/README_CN.md#1104-二叉树寻路
24542456
[1105]:Problemset/1105-Filling%20Bookcase%20Shelves/README_CN.md#1105-填充书架
@@ -4127,6 +4129,7 @@
41274129
[1091l]:https://leetcode.cn/problems/shortest-path-in-binary-matrix/
41284130
[1093l]:https://leetcode.cn/problems/statistics-from-a-large-sample/
41294131
[1094l]:https://leetcode.cn/problems/car-pooling/
4132+
[1095l]:https://leetcode.cn/problems/find-in-mountain-array/
41304133
[1103l]:https://leetcode.cn/problems/distribute-candies-to-people/
41314134
[1104l]:https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/
41324135
[1105l]:https://leetcode.cn/problems/filling-bookcase-shelves/

0 commit comments

Comments
 (0)