def approximate_search(query, data, centroids, hash, R= 100): if R > len(data): R = len(data) num_chunks = centroids.shape[0] # Google uses 256 num_centroids = centroids.shape[1] # build lookup table -- # lookup[i, j] == squared euclidean distance between the jth centroid of # the ith chunk and the query point lookup = np.empty(tuple([num_chunks] + [num_centroids])) for i, chunk in enumerate(centroids): # [optimization] parallelize this for j, centroid in enumerate(chunk): lookup[i, j] = scp.spatial.distance.sqeuclidean(query, centroid) # calculate approximate distance approx_dists = [(float("inf"), -1)]*R for index, h_i in hash: approx_dist = 0 for i, j in enumerate(h_i): approx_dist += lookup[i, j] # [optimization] early out: # if approx_dist >= approx_dists[0]: # break if approx_dist < approx_dists[0]: heapq._heapreplace_max(approx_dists, (approx_dist, index)) return approx_dists
def kSmallest(lst, k): ############################# # PLEASE ADD YOUR CODE HERE # ############################# heap = lst[:k] heapq._heapify_max(heap) for i in range(k, n): if(heap[0]>lst[i]): heapq._heapreplace_max(heap, lst[i]) return heap
def findKthMin(arr, k): maxHeap = [] for i in range(0, k): maxHeap.append(arr[i]) heapq._heapify_max(maxHeap) for i in range(k, len(arr)): if arr[i] < maxHeap[0]: heapq._heapreplace_max(maxHeap, arr[i]) print(maxHeap[0]) return maxHeap[0]
def getLeastNumbers(self, arr, k: int): if k == 0: return [] temp = arr[:k] heapq._heapify_max(temp) for i in range(k, len(arr)): if temp[0] > arr[i]: # 不能直接使用_heapreplace_max函数,因为这个函数不管arr[i]是否小于最大堆顶,都会先删除堆顶,再将arr[i]加入堆 heapq._heapreplace_max(temp, arr[i]) return temp
def lastStoneWeight(self, stones: List[int]) -> int: _heapify_max(stones) while len(stones) > 1: x = _heappop_max(stones) y = stones[0] if x == y: _heappop_max(stones) else: _heapreplace_max(stones, x - y) return stones[0] if stones else 0
def kSmallest(lst, k): ############################# if lst == None: return li = lst[:k] heapq._heapify_max(li) for i in range(k, len(lst)): if lst[i] < li[0]: heapq._heapreplace_max(li, lst[i]) return li
def addc(c): f = fitness(c) s = sum(c) t = (f, c) maxs = h[0][0] if (f in wtdic): return if f < maxs: del wtdic[maxs] pq._heapreplace_max(h, t) wtdic[f] = s
def get_kleast_heap(lst: list, k: int) -> list: # also can construct all the data to a min heap. if not lst or k > len(lst) or k < 1: return [] container = lst[:k] import heapq heapq._heapify_max(container) for i in lst[k:]: if i < container[0]: heapq._heapreplace_max(container, i) # return container # return sorted (convenient for testing) return [heapq._heappop_max(container) for i in range(k)]
def solution(no, works): result = 0 works.sort(reverse = True) while no > 0: if works[0] == 0: break else: heapq._heapreplace_max(works, works[0]-1) no -= 1 for work in works: result += work ** 2 return result
def kSmallest(arr, k): li = [] count = 0 for ele in arr: if count == k: break else: count += 1 li.append(ele) heapq._heapify_max(li) for i in range(k, len(arr)): if arr[i] < li[0]: heapq._heapreplace_max(li, arr[i]) return li
def smallestDistancePair(self, nums: List[int], k: int) -> int: self.k = k i = 0 L = len(nums) while i < L - 1: j = i + 1 while j < L: d = nums[i] - nums[j] if len(self.distance) < self.k: self.distance.append(abs(d)) heapq._heapify_max(self.distance) elif d < self.distance[0]: heapq._heapreplace_max(self.distance, d) j += 1 i += 1 return self.distance[0]
def median(arr): max_heap = [] min_heap = [] results = [] heapq.heappush(min_heap, arr[0]) results.append(arr[0]) for i in arr[1:]: max_l = len(max_heap) min_l = len(min_heap) if max_l > min_l: if i < max_heap[0]: temp = max_heap[0] heapq._heapreplace_max(max_heap, i) min_heap.heappush(temp) else: heapq.heappush(min_heap, i) results.append((max_heap[0] + min_heap[0]) / 2) continue elif max_l < min_l: if i > min_heap[0]: temp = min_heap[0] heapq.heapreplace(min_heap, i) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) else: max_heap.append(i) heapq._siftdown_max(max_heap, 0, max_l) results.append((max_heap[0] + min_heap[0]) / 2) continue else: if i > min_heap[0]: heapq.heappush(min_heap, i) results.append(min_heap[0]) else: max_heap.append(i) heapq._siftdown_max(max_heap, 0, max_l) results.append(max_heap[0]) return results
def step(self, arg, value, limit): if limit is not None and limit <= 0: raise Exception('ArgMin\'s limit must be positive.') if len(self.result) > 0: if DeFactoType(value) != DeFactoType(self.result[0][0]): raise Exception('ArgMin got incompatible values: %s vs %s' % (repr(value), repr(self.result[0][0]))) if limit is None or len(self.result) < limit - 1: self.result.append((value, arg)) elif len(self.result) == limit - 1: self.result.append((value, arg)) heapq._heapify_max(self.result) elif len(self.result) == limit: if self.result[0][0] > value: heapq._heapreplace_max(self.result, (value, arg)) else: print("ArgMin error:", self.result, arg, value, limit) raise Exception('ArgMin error')
def kSmallest(lst, k): maxHeap = lst[:k] print(maxHeap) # heapify_max is heapq functionality which creates max_heap with largest element at first # O(K logK) heapq._heapify_max(maxHeap) print(maxHeap) n = len(lst) # (n-K)logK for i in range(k, n): print(lst[i], maxHeap[0]) if lst[i] < maxHeap[0]: heapq._heapreplace_max(maxHeap, lst[i]) print(maxHeap) # overall time Complexity : (n-K)logK + K logK => n LogK return maxHeap
def max_heap(): arr = [20, -10, 44, 17, 8] heapq._heapify_max(arr) # [44, 17, 20, -10, 8] print(arr) max_el = heapq._heappop_max(arr) print(max_el) # 44 print(arr) # [20, 17, 8, -10] # _heapreplace_max is similar to heappushpop max_el = heapq._heapreplace_max(arr, 12) print(max_el) # 200 print(arr) # [17, 12, 8, -10]
# equal size case if len(smallMaxHeap) == len(bigMinHeap): if i <= leftVal: _heappushMax(smallMaxHeap, i) else: heappush(bigMinHeap, i) continue # left one is bigger if len(smallMaxHeap) > len(bigMinHeap): if i >= leftVal: heappush(bigMinHeap, i) elif i >= smallMaxHeap[0] and i <= bigMinHeap[0]: heappush(bigMinHeap, i) elif i <= leftVal: hold = _heapreplace_max(smallMaxHeap, i) heappush(bigMinHeap, hold) continue # right one is bigger if len(smallMaxHeap) < len(bigMinHeap): if i <= bigMinHeap[0]: _heappushMax(smallMaxHeap, i) elif i >= bigMinHeap[0]: hold = heapreplace(bigMinHeap, i) _heappushMax(smallMaxHeap, hold) continue
def replace_root(self, elem: int): return heapq._heapreplace_max(self.heap, elem)
# -*- coding: utf-8 -*- """ Created on Sat Jun 6 19:28:44 2020 @author: cheerag.verma """ #max heap inbuilts function import heapq li = [10,5,4,3,22,9,31] heapq._heapify_max(li) # builds the max heap heapq._heappop_max(li) # pops the max element from the list heapq._heapreplace_max(li,0) # pops out the top element and replace it with 0 (element) and perform downheapify # insert element in the max heap li.append(20) heapq._siftdown_max(li,0,len(li)-1) #li,startposition,endposition heapq._heapreplace_max(li)
import heapq li = [1, 5, 4, 7, 8, 9, 2, 3] heapq._heapify_max(li) print(li) print(heapq._heappop_max(li)) print(li) heapq._heapreplace_max(li, 0) print(li) #No Inbuilt Push Function. Implement via own as shown below li.append(6) heapq._siftdown_max(li, 0, len(li) - 1) print(li)
import heapq lst = [3, 8, 5, 4, 6, 9, 7] heapq._heapify_max(lst) # heapify for max heap print(*lst) heapq._heappop_max(lst) # max element popped and lst is re-heapified print(*lst) heapq._heapreplace_max(lst, 0) # replace the max element of the heap print(*lst) # to push into a max heap- is different lst.append(1) heapq._siftdown_max( lst, 0, len(lst) - 1) # heapq._siftdown_max(heap,startpos of heap,pos of inserted element) print(*lst) # also we can use heapq._siftup_max(heap,pos)
d["b"] ########binary search############### import bisect a = [1,3,5] bisect.bisect_left(a, 2) #1. log(n) a.insert(1,2) #a = [1,2,3,5] bisect.insort_left(a, 2) # for insert, ####priority queue with heapq import heapq lo = [] hi = [] heapq.heapify(lo) #n*log(n), in place change, lo[0] is the smallest one, lo.pop(0) heapq._heapify_max(hi) heapq._heapreplace_max(a, 7) heapq._heappop_max(a) def _heappush_max(heap, item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) heapq.heappush(lo, value) #log(n) for push and pop value = heapq.heappop(lo) heapsort(iterable): h = [] for value in iterable: heappush(h, value) return [heappop(h) for i in range(len(h))] ########deal with file ####################
import heapq li = [1, 5, 4, 7, 8, 9, 2, 3] heapq._heapify_max(li) ##building max heap print(li) print(heapq._heappop_max(li)) ##removing max element from max heaap print(li) heapq._heapreplace_max(li, 0) ##removing max and replacing it with other element print(li)
def median(_add=None, _remove=None): result = None max_l = len(max_heap) min_l = len(min_heap) if _add: if not min_l: heapq.heappush(min_heap, _add) elif max_l > min_l: if _add < max_heap[0]: temp = max_heap[0] heapq._heapreplace_max(max_heap, _add) heapq.heappush(min_heap, temp) else: heapq.heappush(min_heap, _add) elif max_l < min_l: if _add > min_heap[0]: temp = min_heap[0] heapq.heapreplace(min_heap, _add) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) else: max_heap.append(_add) heapq._siftdown_max(max_heap, 0, max_l) else: if _add > min_heap[0]: heapq.heappush(min_heap, _add) else: max_heap.append(_add) heapq._siftdown_max(max_heap, 0, max_l) max_l = len(max_heap) min_l = len(min_heap) if _remove: if min_heap and _remove >= min_heap[0]: indx = min_heap.index(_remove) min_heap[indx] = min_heap[-1] min_heap.pop() if indx < len(min_heap): heapq._siftup(min_heap, indx) heapq._siftdown(min_heap, 0, indx) elif max_heap and _remove <= max_heap[0]: indx = max_heap.index(_remove) max_heap[indx] = max_heap[-1] max_heap.pop() if indx < len(max_heap): heapq._siftup_max(max_heap, indx) heapq._siftdown_max(max_heap, 0, indx) max_l = len(max_heap) min_l = len(min_heap) if max_l - min_l >= 2: temp = max_heap[0] heapq._heappop_max(max_heap) heapq.heappush(min_heap, temp) elif min_l - max_l >= 2: temp = min_heap[0] heapq.heappop(min_heap) max_heap.append(temp) heapq._siftdown_max(max_heap, 0, max_l) min_l = len(min_heap) max_l = len(max_heap) if min_l == max_l: result = (max_heap[0] + min_heap[0]) / 2 # print(f'debug: max_heap[0]: {max_heap[0]} min_heap[0]:{min_heap[0]}') elif min_l > max_l: result = min_heap[0] else: result = max_heap[0] return result
def update_event(self, inp=-1): self.set_output_val(0, heapq._heapreplace_max(self.input(0), self.input(1)))
import heapq data = [1, 5, 3, 2, 8, 5] heapq._heapify_max(data) print(data) print(heapq._heappop_max(data)) print(data) heapq._heapreplace_max(data, 12) print(data) heapq.heappush(data, 15) print(data) heapq._heapify_max(data) print(data)
import heapq # 虽然显示红色,但是可以运行 from heapq import _heapify_max, _heappop_max, _heapreplace_max list = [1, 2, 3] _heapify_max(list) print(list) print(_heappop_max(list)) print(list) print(_heapreplace_max(list, 3)) print(list)