find-peak-element.dart (732B)
1 //Given a list of integers nums return a value in the list which 2 //is a local peak defined as n[i] > n[i-1] and n[i] > n[i+1]. 3 4 //To do this I iterated through the list checking if the left and right values 5 //were higher or lower than the current. If both are higher then return i. 6 7 //Time complexity is O(n) 8 9 //Time: 227ms Beats: 93.75% 10 //Memory: 143.7MB Beats: 12.50% 11 class Solution { 12 int findPeakElement(List<int> nums) { 13 if(nums.length <= 1){ 14 return 0; 15 } 16 for(int i = 1 ; i < nums.length - 1 ; ++i){ 17 if(nums[i] > nums[i - 1] && nums[i] > nums[i + 1]){ 18 return i; 19 } 20 } 21 if(nums[0] < nums[1]){ 22 return nums.length - 1; 23 } 24 return 0; 25 } 26 }