Last active
May 2, 2019 22:30
-
-
Save taixingbi/e5400d306a04e764b894cbd68c2888c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
O( logn ) | |
* find a number | |
private int binarySearch(int [] array, int key){ | |
int l= 0; | |
int r= array.length-1; | |
int m; | |
while(l<=r){ | |
m= (l+r)/2; | |
if(key<array[m]){ | |
r= m-1; | |
} | |
else if( array[m] < key){ | |
l= m+1; | |
} | |
else{ | |
return m; | |
} | |
} | |
return -1; | |
} | |
* find lower bound | |
private int binarySearch(int [] array, int key){ | |
int l= 0, r= array.length-1, m; | |
while(l<=r){ | |
m= (l+r)/2; | |
if(key<array[m]){ | |
r= m-1; | |
} | |
else if( array[m] < key){ | |
l= m+1; | |
} | |
else return m; | |
} | |
return -1; | |
} | |
nums= [0,1,2,3,3,4,5] | |
val= 3 | |
print binarySearch(nums, val) | |
lower bound: 3 | |
upper bound: 4 | |
* insert a val | |
private static int binarySearch(int [] array, int key){ | |
int l= 0; | |
int r= array.length-1; | |
int m; | |
while(l<=r){ | |
m= (l+r)/2; | |
System.out.println(l+"/"+m+"/"+r); | |
if(key<array[m]){ | |
r= m-1; | |
} | |
else if( array[m] < key){ | |
l= m+1; | |
} | |
else{ | |
l= m; | |
break; | |
} | |
} | |
return l; | |
} | |
nums= [0,1,2,3,3,4,5] | |
val= 3 | |
print binarySearch(nums, val) | |
2 | |
val= 2.5 | |
print binarySearch(nums, val) | |
l,m,r | |
0 1 2 | |
2 2 2 | |
3 2 2 | |
3 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* 658. Find K Closest Elements | |
https://leetcode.com/problems/find-k-closest-elements/ | |
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. | |
Example 1: | |
Input: [1,2,3,4,5], k=4, x=3 | |
Output: [1,2,3,4] | |
Example 2: | |
Input: [1,2,3,4,5], k=4, x=-1 | |
Output: [1,2,3,4] | |
class Solution { | |
public int findMin(int[] nums) { | |
if(nums.length==1) return nums[0]; | |
int l=0, r=nums.length-1; | |
while(l<=r) { | |
if(nums[l]<nums[r]) return nums[l]; | |
int m= (l+r)/2; | |
if( nums[m] > nums[m+1]) return nums[m+1]; | |
if( nums[m-1] > nums[m]) return nums[m]; | |
if(nums[l] < nums[m]) l= m+1; | |
else r=m-1; | |
} | |
return -1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69. Sqrt(x) | |
https://leetcode.com/problems/sqrtx/ | |
Compute and return the square root of x, where x is guaranteed to be a non-negative integer. | |
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. | |
Example 1: | |
Input: 4 | |
Output: 2 | |
Example 2: | |
Input: 8 | |
Output: 2 | |
Explanation: The square root of 8 is 2.82842..., and since | |
the decimal part is truncated, 2 is returned. | |
class Solution(object): | |
def mySqrt(self, x): | |
""" | |
:type x: int | |
:rtype: int | |
""" | |
l = 0 | |
r = x/2 +1 | |
while(l <= r): | |
m = (r + l)/2 | |
if x < m*m: r = m - 1 | |
elif m*m < x: l = m + 1 | |
else: return m | |
return l - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
852. Peak Index in a Mountain Array | |
https://leetcode.com/problems/peak-index-in-a-mountain-array/ | |
Let's call an array A a mountain if the following properties hold: | |
A.length >= 3 | |
There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] | |
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]. | |
Example 1: | |
Input: [0,1,0] | |
Output: 1 | |
Example 2: | |
Input: [0,2,1,0] | |
Output: 1 | |
class Solution(object): | |
def peakIndexInMountainArray(self, A): | |
""" | |
:type A: List[int] | |
:rtype: int | |
""" | |
l, r= 0, len(A)-1 | |
while l < r : | |
m= (l+r)/2 | |
if A[m-1] < A[m] > A[m+1]: return m | |
elif A[m-1] < A[m] < A[m+1]: l=m | |
#A[m-1] > A[m] > A[m+1] or A[m-1] > A[m] < A[m+1] | |
else: r= m | |
162. Find Peak Element | |
https://leetcode.com/problems/find-peak-element/ | |
A peak element is an element that is greater than its neighbors. | |
Given an input array nums, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞. | |
Example 1: | |
Input: nums = [1,2,3,1] | |
Output: 2 | |
Explanation: 3 is a peak element and your function should return the index number 2. | |
Example 2: | |
Input: nums = [1,2,1,3,5,6,4] | |
Output: 1 or 5 | |
Explanation: Your function can return either index number 1 where the peak element is 2, | |
or index number 5 where the peak element is 6. | |
class Solution(object): | |
def findPeakElement(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
nums= [float("-inf")]+ nums + [float("-inf") ] | |
l, r= 0, len(nums)-1 | |
while l < r : | |
m= (l+r)/2 | |
if nums[m-1] < nums[m] > nums[m+1]: return m-1 | |
elif nums[m-1] < nums[m] < nums[m+1]: l= m | |
#nums[m-1] > nums[m] > nums[m+1] or nums[m-1] > nums[m] < nums[m+1] | |
else: r= m | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4. Median of Two Sorted Arrays | |
https://leetcode.com/problems/median-of-two-sorted-arrays/ | |
There are two sorted arrays nums1 and nums2 of size m and n respectively. | |
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). | |
You may assume nums1 and nums2 cannot be both empty. | |
Example 1: | |
nums1 = [1, 3] | |
nums2 = [2] | |
The median is 2.0 | |
Example 2: | |
nums1 = [1, 2] | |
nums2 = [3, 4] | |
The median is (2 + 3)/2 = 2.5 | |
class Solution(object): | |
def findMedianSortedArrays(self, nums1, nums2): | |
""" | |
:type nums1: List[int] | |
:type nums2: List[int] | |
:rtype: float | |
""" | |
def findKth(a, b, k): | |
if not a: return b[k] | |
if not b: return a[k] | |
ia, ib = len(a)/2 , len(b)/2 | |
ma, mb = a[ia], b[ib] | |
# when k is bigger than the sum of a and b's median indices | |
if ia + ib < k: | |
# if a's median is bigger than b's, b's first half doesn't include k | |
if ma > mb: return findKth(a, b[ib + 1:], k - ib - 1) | |
else: return findKth(a[ia + 1:], b, k - ia - 1) | |
# when k is smaller than the sum of a and b's indices | |
else: | |
# if a's median is bigger than b's, a's second half doesn't include k | |
if ma > mb: return findKth(a[:ia], b, k) | |
else: return findKth(a, b[:ib], k) | |
l = len(nums1) + len(nums2) | |
if l % 2 == 1: return findKth(nums1, nums2, l/2) | |
else: return ( findKth(nums1, nums2, l/2) + findKth(nums1, nums2, l/2-1) ) / 2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment