def minSubsequences(A, B): v = [[] for i in range(26)] minIndex = -1 cnt = 1 j = 0 flag = 0 for i in range(len(A)): p = ord(A[i]) - 97 v[p].append(i) while (j < len(B)): p = ord(B[j]) - 97 k = upper_bound(v[p], minIndex) if (len(v[p]) == 0): flag = 1 break if (k != len(v[p])): minIndex = v[p][k] j = j + 1 else: cnt = cnt + 1 minIndex = -1 if (flag == 1): return -1 return cnt
def binaryMedian(m, r, d): mi = m[0][0] mx = 0 for i in range(r): if m[i][0] < mi: mi = m[i][0] if m[i][d - 1] > mx: mx = m[i][d - 1] desired = (r * d + 1) // 2 while (mi < mx): mid = mi + (mx - mi) // 2 place = [0] # Find count of elements smaller than mid for i in range(r): j = upper_bound(m[i], mid) place[0] = place[0] + j if place[0] < desired: mi = mid + 1 else: mx = mid print("Median is", mi) return
def findKth(self, a, m, n, mi, mx, desired): while mi < mx: mid = (mi + mx) // 2 left = 0 for i in range(m): tmp = upper_bound(a[i], mid) left += tmp if left < desired: mi = mid + 1 else: mx = mid return mi
def binaryMedian(m, r, d): mi = m[0][0] mx = 0 for i in range(r): if m[i][0] < mi: mi = m[i][0] if m[i][d - 1] > mx: mx = m[i][d - 1] desired = (r * d + 1) // 2 while (mi < mx): mid = mi + (mx - mi) // 2 place = [0] for i in range(r): j = upper_bound(m[i], mid) place[0] = place[0] + j if place[0] < desired: mi = mid + 1 else: mx = mid print('Median = ', mi) return
def findMedian(self, A): n = len(A) m = len(A[0]) mi = 0 ma = 0 for i in range(n): if mi>A[i][0]: mi = A[i][0] if ma<A[i][-1]: ma = A[i][-1] desired = (m*n+1)//2 while mi<ma: place = [0] mid = mi+(ma-mi)//2 for i in range(n): u = upper_bound(A[i],mid) place[0]+=u if place[0]<desired: mi = mid+1 else: ma = mid return ma
def findFrequency(arr,n,left,right,element): #lower bound gives takes list of locations #where element is present in orgnal array #and the left range # and returns the index of element from #that list which is greater equal to given #left range. a=lower_bound(store[element],left) #upper bound gives takes list of locations #where element is present in orgnal array #and the right range # and returns the index of element from #that list which is less than or equal to #given right range. b=upper_bound(store[element],right) #return last index where element exists #in range - the first index where it #exist of the list of occurances return b-a
def median(arr, r, c): mn = float('inf') mx = -float('inf') for i in range(r): mn = min(arr[i][0], mn) mx = max(arr[i][-1], mn) #print(mn,mx) des = (r * c + 1) // 2 while mn < mx: temp = 0 mid = mn + (mx - mn) // 2 #print(mn,mx) for i in range(r): j = upper_bound(arr[i], mid) #print(j,mid) temp += j if temp < des: mn = mid + 1 else: mx = mid return mn
def binaryMedian(m, r, c): min = sys.maxsize max = -sys.maxsize for i in range(0, r): if m[i][0] < min: min = m[i][0] if m[i][c - 1] > max: max = m[i][c - 1] median = (r * c + 1) // 2 while (min < max): mid = min + (max - min) // 2 place = [0] for i in range(r): j = upper_bound(m[i], mid) place[0] = place[0] + j if place[0] < median: min = mid + 1 else: max = mid return min
def count_k(arr, n, k): # Variable to store final answer ans = 0 # Loop to find prefix-sum for i in range(1, n): arr[i] += arr[i - 1] if (arr[i] > k or arr[i] < -1 * k): ans += 1 if (arr[0] > k or arr[0] < -1 * k): ans += 1 # Sorting prefix-sum array arr = sorted(arr) # Loop to find upper_bound # for each element for i in range(n): ans += n - upper_bound(arr, arr[i] + k) # Returning final answer return ans
def findMedian(self, A): R = len(A) C = len(A[0]) mi = A[0][0] mx = 0 for i in range(R): if A[i][0] < mi: mi = A[i][0] if A[i][C - 1] > mx: mx = A[i][C - 1] desired = (R * C + 1) / 2 while mi < mx: mid = mi + (mx - mi) // 2 place = [0] for i in range(R): j = upper_bound(A[i], mid) place[0] = place[0] + j if place[0] < desired: mi = mid + 1 else: mx = mid return mi
def findMedian(arr): n = len(arr) m = len(arr[0]) # Find min and max element of matrix min_el, max_el = arr[0][0], 0 for i in range(n): min_el = min(min_el, arr[i][0]) max_el = max(max_el, arr[i][-1]) target = (1 + n*m) // 2 while min_el <= max_el: mid = min_el + (max_el - min_el) // 2 count = 0 # count no_of_elements smaller than mid for i in range(n): count += upper_bound(arr[i], mid) if count < target: min_el = mid + 1 else: max_el = mid - 1 return min_el
def binary_median(m, r, d): mi = m[0][0] mx = 0 for i in range(r): if m[i][0] < mi: mi = m[i][0] if m[i][d - 1] > mx: mx = m[i][d - 1] desired = (r * d + 1) // 2 while mi < mx: mid = mi + (mx - mi) // 2 place = 0 for i in range(r): j = upper_bound(m[i], mid) print("J: ", j, "M[i] ", m[i], "Mid:", mid) place = place + j print("Place: ", place, "Desired", desired) if place < desired: mi = mid + 1 else: mx = mid print("Median is", mi) return
from bisect import bisect_right as upper_bound #arr = [[1, 3, 5], [2, 6, 9], [3, 6, 9]] arr = [[1],[3],[3]] r = len(arr) c = len(arr[0]) lg = r*c // 2 mini = arr[0][0] maxi = 0 for i in range(r): if arr[i][0] < mini: mini = arr[i][0] if arr[i][c-1] > maxi: maxi = arr[i][c-1] print(mini,maxi) while mini < maxi: mid = mini + (maxi - mini) // 2 j = 0 for i in range(r): j += upper_bound(arr[i],mid) if j > lg: maxi = mid else: mini = mid + 1 print(f"Median is {mini}")