Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Checks:
- 1. The whole array can neither be strictly increasing or strictly decreasing entirely.
- 2. The strictly decreasing part should come after the strictly increasing part and together they should make up the entire array
- '''
- #1. One Pointer
- class Solution:
- def validMountainArray(self, arr: List[int]) -> bool:
- i, n = 0, len(arr)
- while i<n-1 and arr[i]<arr[i+1]:
- i += 1
- if i==0 or i==n-1:
- return False
- while i<n-1 and arr[i]>arr[i+1]:
- i += 1
- return i==n-1
- #2. Two pointer
- #credits to lee215 solution for idea
- class Solution:
- def validMountainArray(self, arr: List[int]) -> bool:
- n = len(arr)
- i, j = 0, n-1
- while i<n-1 and arr[i]<arr[i+1]:
- i += 1
- while j>0 and arr[j]<arr[j-1]:
- j -= 1
- return i == j and 0 < i < n-1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement