-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-indices-of-stable-mountains_3285.py
73 lines (40 loc) · 1.65 KB
/
find-indices-of-stable-mountains_3285.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python3
# There are n mountains in a row, and each mountain has a height. You are given an integer array height where height[i] represents the height of mountain i, and an integer threshold.
# A mountain is called stable if the mountain just before it (if it exists) has a height strictly greater than threshold. Note that mountain 0 is not stable.
# Return an array containing the indices of all stable mountains in any order.
# Example 1:
# Input: height = [1,2,3,4,5], threshold = 2
# Output: [3,4]
# Explanation:
# Mountain 3 is stable because height[2] == 3 is greater than threshold == 2.
# Mountain 4 is stable because height[3] == 4 is greater than threshold == 2.
# Example 2:
# Input: height = [10,1,10,1,10], threshold = 3
# Output: [1,3]
# Example 3:
# Input: height = [10,1,10,1,10], threshold = 10
# Output: []
# Constraints:
# 2 <= n == height.length <= 100
# 1 <= height[i] <= 100
# 1 <= threshold <= 100
# ---------------------------------------Runtime 49 ms Beats 100.00% Memory 16.66 MB Beats 100.00%---------------------------------------
from typing import List
class Solution:
def stableMountains(self, height: List[int], threshold: int) -> List[int]:
return [
i for i in range(len(height)) if i and height[i - 1] > threshold
]
sol = Solution()
# Input: height = [1,2,3,4,5], threshold = 2
# Output: [3,4]
height = [1, 2, 3, 4, 5]
threshold = 2
res = sol.stableMountains(height, threshold)
assert res == [3, 4], res
# Input: height = [10,1,10,1,10], threshold = 3
# Output: [1,3]
height = [10, 1, 10, 1, 10]
threshold = 3
res = sol.stableMountains(height, threshold)
assert res == [1, 3], res