-
Leetcode 852. Peak Index in a Mountain Array개발/Leetcode 2023. 7. 25. 09:36
https://leetcode.com/problems/peak-index-in-a-mountain-array/description/
Peak Index in a Mountain Array - LeetCode
Can you solve this real interview question? Peak Index in a Mountain Array - An array arr a mountain if the following properties hold: * arr.length >= 3 * There exists some i with 0 < i < arr.length - 1 such that: * arr[0] < arr[1] < ... < arr[i - 1] < arr
leetcode.com
문제
주어진 배열 다음과 같은 조건을 만족하면 산이 된다.
- arr.length >= 3
- 0 <= i < arr.length -1 이라면, arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
- arr[i] > arr[i + 1] > ... > arr[arr.length - 1
배열 arr가 주어질 때 arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 를 만족하는 i를 찾아 반환하라
반드시 O(log(arr.length)) 시간 복잡도로 해결해야한다!
코드
#Pythonclass Solution:def peakIndexInMountainArray(self, arr: List[int]) -> int:return arr.index(max(arr))// C#public class Solution{public int PeakIndexInMountainArray(int[] arr){int peek = 0;
for (int i = 1 ; i < arr.Length; i ++ ) {if ( arr[i] > arr[peek] ) {peek = i;}}return peek;}}
이래도 괜찮은 걸까..
'개발 > Leetcode' 카테고리의 다른 글
Leetcode 1870. Minimum Speed to Arrive on Time (0) 2023.07.26 Leetcode 50. Pow(x, n) (0) 2023.07.24 Leetcode 735. Asteroid Collision (0) 2023.07.20 Leetcode 435. Non-overlapping Intervals (0) 2023.07.19 Leetcode 146. LRU Cache (0) 2023.07.18