class Solution {
public int findPeakElement(int[] nums) {
// 1 2 1 3 1
// nums[i] != nums[i + 1] for all valid i.
// why comare your neighbor is the ans?
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left)/2;
if (nums[mid] < nums[mid+1]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}
class Solution {
fun findPeakElement(nums: IntArray): Int {
var left = 0;
var right = nums.size - 1;
while (left < right) {
var mid = left + (right - left)/2;
if (nums[mid] < nums[mid+1]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}