Skip to content

Commit 1d96962

Browse files
committed
4_Median_of_Two_Sorted_Arrays
1 parent c13b0e8 commit 1d96962

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. Hash O(n) and O(n) space<br>2. Sort and search with two points O(n) and O(1) space |
1010
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/2_Add_Two_Numbers.py) | Note the carry from lower digit. |
1111
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/3_Longest_Substring_Without_Repeating_Characters.py) |1. Check every possible substring O(n^2) <br>2. Remember the character index and current check pos, if character index >= current pos, then there is duplicate |
12+
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/.py) | 1. Merge two sorted lists and compute median, O(m + n) and O(m + n)<br>2. An extension of median of two sorted arrays of equal size problem|
1213
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/5_Longest_Palindromic_Substring.py) | [Background knowledge](http://en.wikipedia.org/wiki/Longest_palindromic_substring)<br>1. DP if s[i]==s[j] and P[i+1, j-1] then P[i,j]<br>2. A palindrome can be expanded from its center<br>3. Manacher algorithm|
1314
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/7_Reverse_Integer.py) | Overflow when the result is greater than 2147483647
1415
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/8_String_to_Integer(atoi).py) | Overflow, Space, and negative number |
@@ -79,4 +80,11 @@
7980
| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
8081
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
8182
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
82-
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
83+
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
84+
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | |
85+
86+
87+
| # | ToDo |
88+
|---| ----- |
89+
90+
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) &hearts; |

python/4_Median_of_Two_Sorted_Arrays.py

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
11
class Solution(object):
2+
# def findMedianSortedArrays(self, nums1, nums2):
3+
# """
4+
# :type nums1: List[int]
5+
# :type nums2: List[int]
6+
# :rtype: float
7+
# """
8+
# p1 = p2 = 0
9+
# ls1 = len(nums1)
10+
# ls2 = len(nums2)
11+
# all_nums = []
12+
# median = 0.0
13+
# while p1 < ls1 and p2 < ls2:
14+
# if nums1[p1] < nums2[p2]:
15+
# all_nums.append(nums1[p1])
16+
# p1 += 1
17+
# else:
18+
# all_nums.append(nums2[p2])
19+
# p2 += 1
20+
# if p1 < ls1:
21+
# while p1 < ls1:
22+
# all_nums.append(nums1[p1])
23+
# p1 += 1
24+
# if p2 < ls2:
25+
# while p2 < ls2:
26+
# all_nums.append(nums2[p2])
27+
# p2 += 1
28+
# # print all_nums
29+
# if (ls1 + ls2) % 2 == 1:
30+
# median = all_nums[(ls1 + ls2) / 2]
31+
# else:
32+
# median = 1.0 * (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2
33+
# return median
34+
235
def findMedianSortedArrays(self, nums1, nums2):
3-
"""
4-
:type nums1: List[int]
5-
:type nums2: List[int]
6-
:rtype: float
7-
"""
8-
p1 = p2 = 0
9-
ls1 = len(nums1)
10-
ls2 = len(nums2)
11-
all_nums = []
12-
median = 0.0
13-
while p1 < ls1 and p2 < ls2:
14-
if nums1[p1] < nums2[p2]:
15-
all_nums.append(nums1[p1])
16-
p1 += 1
36+
N1, N2 = len(nums1), len(nums2)
37+
if N1 < N2:
38+
nums1, N1, nums2, N2 = nums2, N2, nums1, N1
39+
l, r = 0, N2 * 2
40+
while l <= r:
41+
mid2 = (l + r) >> 1
42+
mid1 = N1 + N2 - mid2
43+
L1 = -sys.maxint - 1 if mid1 == 0 else nums1[(mid1 - 1) >> 1]
44+
L2 = -sys.maxint - 1 if mid2 == 0 else nums2[(mid2 - 1) >> 1]
45+
R1 = sys.maxint if mid1 == 2 * N1 else nums1[mid1 >> 1]
46+
R2 = sys.maxint if mid2 == 2 * N2 else nums2[mid2 >> 1]
47+
if L1 > R2:
48+
l = mid2 + 1
49+
elif L2 > R1:
50+
r = mid2 - 1
1751
else:
18-
all_nums.append(nums2[p2])
19-
p2 += 1
20-
if p1 < ls1:
21-
while p1 < ls1:
22-
all_nums.append(nums1[p1])
23-
p1 += 1
24-
if p2 < ls2:
25-
while p2 < ls2:
26-
all_nums.append(nums2[p2])
27-
p2 += 1
28-
# print all_nums
29-
if (ls1 + ls2) % 2 == 1:
30-
median = all_nums[(ls1 + ls2) / 2]
31-
else:
32-
median = 1.0 * (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2
33-
return median
52+
return (max(L1, L2) + min(R1, R2)) / 2.0
3453

3554

3655
if __name__ == '__main__':

0 commit comments

Comments
 (0)