728x90
난이도 : easy
위의 사진과 같이, mountaiin array가 성립하면 True를 return하는 문제
class Solution:
def validMountainArray(self, arr: List[int]) -> bool:
peak = max(arr)
top = arr.index(peak)
left = 0
right = len(arr) - 1
if top == left or top == right:
return False
while left < top:
if arr[left] >= arr[left + 1]:
return False
left += 1
while right > top:
if arr[right] >= arr[right - 1]:
return False
right -= 1
return True
- 산의 높이를 peak라고 생각하고, 지점을 top으로 설정
- 리스트 형태의 처음과 끝을 생각하고, left와 right를 각각 0과 arr-1로 설정
- top 위치가 left나 right가 되면 False
- left에서 top까지 arr의 값이 작아지거나 같으면 False
- right에서 top까지 arr의 값이 크거나 같으면 False
Runtime : 161ms (70.35%)
Memory : 19.02MB (45.91%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python] 1299. Replace Elements with Greatest Element on Right Side (0) | 2025.01.28 |
---|---|
[Python] 1462. Course Schedule IV (0) | 2025.01.28 |
[Python] 1346. Check If N and Its Double Exist (0) | 2025.01.26 |
[Python] 2425. Bitwise XOR of All Pairings (0) | 2025.01.25 |
[Python] 2429. Minimize XOR (0) | 2025.01.24 |