We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3dbfa63 commit ea24c83Copy full SHA for ea24c83
162. Find Peak Element.cpp
@@ -0,0 +1,34 @@
1
+TC = O (nlogn)
2
+class Solution {
3
+public:
4
+ int findPeakElement(vector<int>& nums) {
5
+ int low = 0 , high = nums.size()-1;
6
+ while(low < high) {
7
+ int mid = (low + high)/2;
8
+ if(nums[mid] > nums[mid+1]) high = mid;
9
+ else low = mid + 1 ;
10
+ }
11
+ return low;
12
13
+};
14
+
15
+TC = O (n)
16
17
18
19
+ return max_element(nums.begin() , nums.end()) - nums.begin() ;
20
21
22
23
+TC = O(n)
24
25
26
27
+ int n = nums.size() , ind = 0;
28
+ for(int i = 0; i < n-1 ; i++){
29
+ if(nums[i] > nums[i+1] ) return i;
30
31
+ return n-1;
32
33
34
0 commit comments