-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.py
More file actions
166 lines (145 loc) · 4.57 KB
/
binarysearch.py
File metadata and controls
166 lines (145 loc) · 4.57 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# #Q1. Binary search
class Soltuion:
def search(self,nums,target):
left, right=0,len(nums)-1
while left<=right:
mid=(left+right)//2
if nums[mid]==target:
return mid
elif nums[mid]<target:
left=mid+1
else:
right=mid-1
return -1
nums=[-1,0,2,4,6,8]
target = 4
sol=Soltuion()
print(sol.search(nums,target))
#Q2. search a 2d matrix
class Solution:
def searchMatrix(self,matrix,target):
if not matrix or not matrix[0]:
return False
m,n=len(matrix),len(matrix[0])
left,right=0,m*n-1
while left<=right:
mid=(left+right)//2
row,col=divmod(mid,n)
mid_val=matrix[row][col]
if mid_val==target:
return True
elif mid_val < target:
left=mid+1
else:
right=mid-1
return False
matrix = [[1,2,4,8],[10,11,12,13],[14,20,30,40]]
target = 10
sol=Solution()
print(sol.searchMatrix(matrix,target))
#Q3. Eating Bananas
import math
class Solution():
def minEatingSpeed(self,piles,h):
left,right=1,max(piles)
result=right
while left<=right:
mid=(left+right)//2
hours=sum(math.ceil(p/mid)for p in piles)
if hours<=h:
result=mid
right=mid-1
else:
left=mid+1
return result
piles = [1,4,3,2]
h = 9
sol=Solution()
print(sol.minEatingSpeed(piles,h))
#Q4. Min in sorted array
class Solution():
def findMin(self,nums):
left,right=0,len(nums)-1
while left<right:
mid=(left+right)//2
if nums[mid]>nums[right]:
left=mid+1
else:
right=mid
return nums[left]
sol=Solution()
nums = [3,4,5,6,1,2]
print(sol.findMin(nums))
#Q5. search in rotated array
class Solution():
def search(self,nums,target):
left,right=0,len(nums)-1
while left<=right:
mid=(left+right)//2
if nums[mid]==target:
return mid
if nums[left]<=nums[mid]:
if nums[left]<=target<nums[mid]:
right=mid-1
else:
left=mid+1
else:
if nums[mid]<target<=nums[right]:
left=mid+1
else:
right=mid-1
return -1
sol=Solution()
nums=[4,5,6,7,0,1,2]
target=0
print(sol.search(nums,target))
#Q5. time based key value store
from collections import defaultdict
import bisect
class TimeMap:
def __init__(self):
self.store=defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.store[key].append((timestamp,value))
def get(self, key: str, timestamp: int) -> str:
if key not in self.store:
return ""
values = self.store[key]
left,right=0,len(values)-1
result=""
while left<=right:
mid=(left+right)//2
if values[mid][0]<=timestamp:
result=values[mid][1]
left=mid+1
else:
right=mid-1
return result
#Q6. median of two sorted arrays
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
if len(nums1)>len(nums2):
nums1,nums2=nums2,nums1
x,y=len(nums1),len(nums2)
low, high = 0, x
while low <= high:
partitionX = (low + high) // 2
partitionY = (x + y + 1) // 2 - partitionX
# Edge handling: -inf for empty partition left, +inf for empty partition right
maxLeftX = float('-inf') if partitionX == 0 else nums1[partitionX - 1]
minRightX = float('inf') if partitionX == x else nums1[partitionX]
maxLeftY = float('-inf') if partitionY == 0 else nums2[partitionY - 1]
minRightY = float('inf') if partitionY == y else nums2[partitionY]
if maxLeftX <= minRightY and maxLeftY <= minRightX:
# Found correct partition
if (x + y) % 2 == 0:
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
else:
return max(maxLeftX, maxLeftY)
elif maxLeftX > minRightY:
# Move towards left in nums1
high = partitionX - 1
else:
# Move towards right in nums1
low = partitionX + 1
raise ValueError("Input arrays are not sorted")