imashutosh51

Nuts and Bolts

May 6th, 2026 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. '''
  2. M-1:
  3. push all the nuts into the set which takes O(n) extra space.make an order array also because we need to do
  4. inplace changes and that inplace also in the order.
  5. go one by one element of the order, check in the nut_set, if it is there means it was there in the nut arr
  6. now if nut is there then Bolt must be there.make both arr at the index as that ch or nut, incremenet idx.
  7.  
  8. T.C -> O(n)
  9. Space Compleixty-> O(n)
  10. '''
  11. class Solution:
  12.     def matchPairs(self, nuts, bolts):
  13.         order = ['!', '#', '$', '%', '&', '*', '?', '@', '^']
  14.        
  15.         nut_set = set(nuts)
  16.        
  17.         idx = 0
  18.         for ch in order:
  19.             if ch in nut_set:
  20.                 nuts[idx] = ch
  21.                 bolts[idx] = ch
  22.                 idx += 1
  23.  
  24. ###We have an extra case here when we are checking the pivot and putting at the end but we don't do it in the quick sort and the reason is we in quick sort we are having the pivot element at the right but here pivot element can be in between also so push at the end first.
  25. '''
  26. M-2
  27. Just apply the quicksort as they are ASCII codes internally and the order they have given is also proper ASCII code increasing order.
  28. T.C: O(n log n) avg
  29. S.C: O(1)
  30. '''
  31.  
  32. def partition(arr, low, high, pivot):
  33.     i = low
  34.     j = low
  35.    
  36.     while j < high:
  37.         if arr[j] < pivot:
  38.             arr[i], arr[j] = arr[j], arr[i]
  39.             i += 1
  40.         elif arr[j] == pivot: #if you got the pivot element,just place it at the end for now because before returning from this fun, we swap with the ith position
  41.             arr[j], arr[high] = arr[high], arr[j]
  42.             j -= 1
  43.         j += 1
  44.    
  45.     arr[i], arr[high] = arr[high], arr[i]
  46.     return i
  47.  
  48.  
  49. def quicksort(nuts, bolts, low, high):
  50.     if low < high:
  51.         # use last bolt as pivot to partition nuts
  52.         pivot_index = partition(nuts, low, high, bolts[high])
  53.        
  54.         # now use matched nut to partition bolts
  55.         partition(bolts, low, high, nuts[pivot_index]) #Here after partition, you will get parition point as pivot_index only because both have same elements but unsorted.so we can say, pivot_index is the point whose left is all smaller than nuts[pivot_index]
  56.         # lace which is paritioned, all left elmenets in bolts are smaller and right are bigger than bolts[pivot_index] and pivot_index is same in both arr.
  57.        
  58.         # recurse
  59.         quicksort(nuts, bolts, low, pivot_index - 1)
  60.         quicksort(nuts, bolts, pivot_index + 1, high)
  61.  
  62.  
  63. class Solution:
  64.     def matchPairs(self, nuts, bolts):
  65.         quicksort(nuts, bolts, 0, len(nuts) - 1)
Advertisement
Add Comment
Please, Sign In to add comment