Buttercola: Zenefits: Middle element
Zenefits: Middle element
在一个整数数组中(无重复元素)找出一个元素,使得其大于所有他前面的数,并且小于所有他后面的数,返回其下标。例如[4,1,2,6,10,7]中,6满足条件,返回下标3。若有多个满足条件,返回一个即可。若无,返回-1
Solution:
Maintain two arrays, left[] and right[]. The left[i] stores the biggest element prior to i. The right[i] stores the minimum value after i.
Then for each element in array nums[i]. If it is greater than the biggest element left to i, and smaller than the smallest element after i. Then nums[i] must be the intermediate element.
Solution:
Maintain two arrays, left[] and right[]. The left[i] stores the biggest element prior to i. The right[i] stores the minimum value after i.
Then for each element in array nums[i]. If it is greater than the biggest element left to i, and smaller than the smallest element after i. Then nums[i] must be the intermediate element.
Read full article from Buttercola: Zenefits: Middle element
No comments:
Post a Comment