leetcode 35 Search Insert Position
Question
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
Solution
Stop not while left==right can let left pointer stop at the closest smaller value, and right pointer stop at the closest bigger value.
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left = 0
right = len(nums) - 1
while left<=right:
mid = (left+right)/2
if nums[mid] == target:
return mid
elif nums[mid] >target:
right = mid -1
else:
left = mid + 1
return left
leetcode 162 Find Peak Element
Question
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
Solution
Questions related to array time exceeded problem is usually solved by binary search. The essential problem of binary search is determine target is on which side, and if it contains the boundary.
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == None or len(nums) == 0:
return -1
left = 0
right = len(nums) - 1
while left < right:
mid = (left + right)/2
if nums[mid] > nums[mid + 1]:
right = mid
elif nums[mid] < nums[mid + 1]:
left = mid + 1
return left
leetcode 275 H-Index II
Question
H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.
According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?
Solution
Key: find the first position where citations[i] < i from the end.
Pay attention to whether include condition that left equals right. Including can let left stop at the position where citations[i] < i.
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
n = len(citations)
left = 0
right = len(citations) - 1
while left <= right:
mid = (left + right) / 2
if citations[mid] > n-mid:
right = mid - 1
elif citations[mid] < n-mid:
left = mid + 1
else:
return citations[mid]
return n - left