GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/004_Median_of_Two_Sorted_Arrays.py at master · terryicoder/leetcode

Expand file tree

/

004_Median_of_Two_Sorted_Arrays.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
60 lines (57 loc) · 2.1 KB
/

004_Median_of_Two_Sorted_Arrays.py

Copy path

File metadata and controls

60 lines (57 loc) · 2.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution(object):
# def findMedianSortedArrays(self, nums1, nums2):
# """
# :type nums1: List[int]
# :type nums2: List[int]
# :rtype: float
# """
# p1 = p2 = 0
# ls1 = len(nums1)
# ls2 = len(nums2)
# all_nums = []
# median = 0.0
# while p1 < ls1 and p2 < ls2:
# if nums1[p1] < nums2[p2]:
# all_nums.append(nums1[p1])
# p1 += 1
# else:
# all_nums.append(nums2[p2])
# p2 += 1
# if p1 < ls1:
# while p1 < ls1:
# all_nums.append(nums1[p1])
# p1 += 1
# if p2 < ls2:
# while p2 < ls2:
# all_nums.append(nums2[p2])
# p2 += 1
# # print all_nums
# if (ls1 + ls2) % 2 == 1:
# median = all_nums[(ls1 + ls2) / 2]
# else:
# median = 1.0 * (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2
# return median
def findMedianSortedArrays(self, nums1, nums2):
# https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation
# https://discuss.leetcode.com/topic/16797/very-concise-o-log-min-m-n-iterative-solution-with-detailed-explanation
ls1, ls2 = len(nums1), len(nums2)
if ls1 < ls2:
return self.findMedianSortedArrays(nums2, nums1)
l, r = 0, ls2 * 2
while l <= r:
mid2 = (l + r) >> 1
mid1 = ls1 + ls2 - mid2
L1 = -sys.maxint - 1 if mid1 == 0 else nums1[(mid1 - 1) >> 1]
L2 = -sys.maxint - 1 if mid2 == 0 else nums2[(mid2 - 1) >> 1]
R1 = sys.maxint if mid1 == 2 * ls1 else nums1[mid1 >> 1]
R2 = sys.maxint if mid2 == 2 * ls2 else nums2[mid2 >> 1]
if L1 > R2:
l = mid2 + 1
elif L2 > R1:
r = mid2 - 1
else:
return (max(L1, L2) + min(R1, R2)) / 2.0
if __name__ == '__main__':
# begin
s = Solution()
print s.findMedianSortedArrays([1, 1], [1, 2])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.