Skip to content

Commit 49e7c36

Browse files
author
Partho Biswas
committed
845. Longest Mountain in Array
1 parent f65e1c5 commit 49e7c36

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ I have solved quite a number of problems from several topics. See the below tabl
175175
|64| **[379. Design Phone Directory](https://tinyurl.com/ybs3848v)** | [Python](https://tinyurl.com/wu6rdaw/379_Design_Phone_Directory.py), [Swift](https://tinyurl.com/wuja3c4/379_Design_Phone_Directory.swift) | | Medium | |
176176
|65| **[280. Wiggle Sort](https://tinyurl.com/ybnjn6fs)** | [Python](https://tinyurl.com/wu6rdaw/280_Wiggle_Sort.py), [Swift](https://tinyurl.com/wuja3c4/280_Wiggle_Sort.swift) | | Medium | |
177177
|66| [246. Strobogrammatic Number](https://tinyurl.com/ycqwsozh) | [Python](https://tinyurl.com/wu6rdaw/246_Strobogrammatic_Number.py), [Swift](https://tinyurl.com/wuja3c4/246_Strobogrammatic_Number.swift) | | Easy | |
178+
|67| **[845. Longest Mountain in Array](https://tinyurl.com/y9h5uah5)** | [Python](https://tinyurl.com/wu6rdaw/845_Longest_Mountain_in_Array.py), [Swift](https://tinyurl.com/wuja3c4/845_Longest_Mountain_in_Array.swift) | | Medium | |
178179

179180

180181
</p>
@@ -751,6 +752,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
751752
|10| [Spiral_Traverse](algoexpert.io/questions/Spiral_Traverse.md) | [Python](algoexpert.io/python/Spiral_Traverse.py) |
752753
|11| [Monotonic_Array](algoexpert.io/questions/Monotonic_Array.md) | [Python](algoexpert.io/python/Monotonic_Array.py) |
753754
|12| [Move_Element_To_End](algoexpert.io/questions/Move_Element_To_End.md) | [Python](algoexpert.io/python/Move_Element_To_End.py) |
755+
|13| [Longest_Peak](algoexpert.io/questions/Longest_Peak.md) | [Python](algoexpert.io/python/Longest_Peak.py) |
754756

755757
</p>
756758
</details>

algoexpert.io/python/Longest_Peak.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def longestPeak(array):
2+
longestPeakLen = 0
3+
i = 1
4+
while i < len(array) - 1:
5+
isPeak = array[i - 1] < array[i] > array[i + 1]
6+
if not isPeak:
7+
i += 1
8+
continue
9+
10+
leftIdx = i - 2
11+
while leftIdx >= 0 and array[leftIdx] < array[leftIdx + 1]:
12+
leftIdx -= 1
13+
14+
rightIdx = i + 2
15+
while rightIdx < len(array) and array[rightIdx - 1] > array[rightIdx]:
16+
rightIdx += 1
17+
18+
currentPeakLen = rightIdx - leftIdx - 1
19+
longestPeakLen = max(longestPeakLen, currentPeakLen)
20+
i = rightIdx
21+
22+
return longestPeakLen
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def longestMountain(self, A):
3+
"""
4+
:type A: List[int]
5+
:rtype: int
6+
"""
7+
longestPeakLen = 0
8+
i = 1
9+
while i < len(A) - 1:
10+
isPeak = A[i - 1] < A[i] > A[i + 1]
11+
if not isPeak:
12+
i += 1
13+
continue
14+
15+
leftIdx = i - 2
16+
while leftIdx >= 0 and A[leftIdx] < A[leftIdx + 1]:
17+
leftIdx -= 1
18+
19+
rightIdx = i + 2
20+
while rightIdx < len(A) and A[rightIdx - 1] > A[rightIdx]:
21+
rightIdx += 1
22+
23+
currentPeakLen = rightIdx - leftIdx - 1
24+
longestPeakLen = max(longestPeakLen, currentPeakLen)
25+
i = rightIdx
26+
27+
return longestPeakLen
28+

0 commit comments

Comments
 (0)