-
Notifications
You must be signed in to change notification settings - Fork 0
/
techniques.py
337 lines (283 loc) · 10.3 KB
/
techniques.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# 2-pointer
def longestValidParentheses(s):
if len(s)==0 or len(s)==1:
return 0
right = len(s)-1
left = 0
while left<=right:
if s[right] == ')' and s[left] =='(':
return right-left
if left == right:
return 0
if s[right]!=')':
right -= 1
if left == right:
return 0
if s[left]!='(':
left += 1
if left == right:
return 0
s = '()))'
print(longestValidParentheses(s))
# four sum problem (two sum approach) using 2-pointers
def fourSum(nums, target):
nums.sort()
res = []
for i in range(len(nums) - 3):
# skip duplicate starting values
if i > 0 and nums[i] == nums[i-1]:
continue
for j in range(i+1, len(nums) - 2):
# skip duplicate starting values
if j > i + 1 and nums[j] == nums[j-1]:
continue
left, right = j + 1, len(nums) - 1
while left < right:
four_sum = nums[i] + nums[j] + nums[left] + nums[right]
if four_sum == target:
res.append([nums[i], nums[j], nums[left], nums[right]])
left += 1
right -= 1
while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1
elif four_sum < target:
left += 1
else:
right -= 1
return res
################################################################################################################################
#Breaking problem to small parts
board = [["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
class Solution(object):
def __init__(self, board):
self.board = board
"""
:type board: List[List[str]]
:rtype: bool
"""
def has_duplicate(self,lst):
seen = set()
for num in lst:
if num!=".":
if num in seen:
return True
seen.add(num)
return False
def checkRow(self,row):
return self.has_duplicate(self.board[row])
def checkColumn(self,col):
column = [self.board[i][col] for i in range(len(self.board))]
return self.has_duplicate(column)
def checkGrid(self,row,col):
subgrid = [self.board[i][j]for i in range(row,row+3) for j in range (col,col+3)]
return self.has_duplicate(subgrid)
def helper(self):
for i in range(9):
if self.checkRow(i) or self.checkColumn(i):
return False
for i in range(0, 9, 3):
for j in range(0, 9, 3):
if not self.checkGrid(i, j):
return False
return True
s = Solution(board)
print(s.helper())
################################################################################################################################
#Backtracking (with,recursion)
def combinationSum(candidates, target):
ans = []
def combinationSumHelper(candidates,curr_index,curr_sum,curr_combi,target):
if curr_sum == target:
ans.append(curr_combi)
return
if curr_sum>target:
return
for i in range(len(candidates)):
combinationSumHelper(candidates , i ,curr_sum + candidates[i] , curr_combi+[candidates[i]] , target)
combinationSumHelper(candidates,0,0,[],target)
return ans
lst = [[2,3,6,7],[5,4,6,7],[0,1,2,3]]
candidates = [2,3,6,7]
target = 7
print(candidates)
print(combinationSum(candidates,target))
################################################################################################################################
#top K elements (Binary Heap)
#Q:- print top K largest elements
import heapq #Binary heap package(min heap)
arr = [1, 23, 12, 9, 30, 2, 50]
K = 3
def top_K_smallest(arr,k):
temp = []
for ele in arr:
heapq.heappush(temp,ele)
if len(temp)>k:
heapq.heappop(temp)
for _ in range(k):
print(heapq.heappop(temp))
print(top_K_smallest(arr,K))
################################################################################################################################
# Hashing Technique
# Refer 'Trie' section
################################################################################################################################
# Sliding window
def lengthOfLongestSubstring(string):
last_idx = {}
max_len = 0
# starting index of current
# window to calculate max_len
start_idx = 0
for i in range(0, len(string)):
# Find the last index of str[i]
# Update start_idx (starting index of current window)
# as maximum of current value of start_idx and last
# index plus 1
if string[i] in last_idx:
start_idx = max(start_idx, last_idx[string[i]] + 1)
# Update result if we get a larger window
max_len = max(max_len, i-start_idx + 1)
# Update last index of current char.
last_idx[string[i]] = i
return max_len
print(lengthOfLongestSubstring('aabccdefghhhj'))
###########################################################################################################################
# Optimized Binary Search
# Question : smallest pair distance
# Method : Similar to binary search and mergeSort combined
# i.e. makes a pair of every consecutive element and then logic is applied
import heapq
class Solution(object):
def __init__(self):
self.ans = {}
def isSmall(self,nums):
left = 0
right = len(nums) - 1
mid = (left+right)//2
if len(nums) == 2:
return [(nums[0],nums[1]) , abs(nums[0] - nums[1])]
pair_left = self.isSmall(nums[0:mid+1])
pair_right = self.isSmall(nums[mid:])
self.ans[pair_left[0]] = pair_left[1]
self.ans[pair_right[0]] = pair_right[1]
if pair_left[1] > pair_right[1]:
return pair_right
else:
return pair_left
def smallestDistancePair(self, nums):
nums.sort()
return self.isSmall(nums)
nums = [1,3,4,6,8,10,11,11]
s = Solution()
print(s.smallestDistancePair(nums))
print(s.ans)
print(list(s.ans))
# Question : 2
def searchInsert(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if target > nums[-1]:
return len(nums)
elif target < nums[0]:
return 0
else:
left = 0
right = len(nums)-1
while left<=right:
m = (left+right)//2
if nums[m] == target:
return m
elif nums[m] > target:
right = m-1
if nums[m] < target:
left = m+1
if nums[left] > target:
return left
if nums[left] < target:
return left+1
nums = [1,3,5,6,7,10,12]
target = 11
print(searchInsert(nums,target))
# Important
class Solution(object):
def kSmallestPairs(self, nums1, nums2, k):
queue = []
def push(i, j):
if i < len(nums1) and j < len(nums2):
heapq.heappush(queue, [nums1[i] + nums2[j], i, j])
push(0, 0)
pairs = []
while queue and len(pairs) < k:
_, i, j = heapq.heappop(queue)
pairs.append([nums1[i], nums2[j]])
push(i, j + 1)
if j == 0:
push(i + 1, 0)
return pairs
def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
numMap = {}
n = len(nums)
# Build the hash table
for i in range(n):
numMap[nums[i]] = i
nums.sort()
left = 0
right = len(nums)-1
while left<right:
sumo = nums[left] + nums[right]
if sumo == target:
return[numMap[nums[left]],numMap[nums[right]]]
elif sumo > target:
right -= 1
else:
left += 1
return False
nums = [2,7,11,15]
target = 9
print(twoSum(nums,target))
words = ["oath","pea","eat","rain"]
words.sort(key = lambda a:len(a))
print(words)
###############################################################################################################################
# Binary search method
def median(nums1 , nums2):
n = len(nums1)
m = len(nums2)
if n > m:
return median(nums2 , nums1)
left = 0
right = n
while left <= right:
partitionA = (left+right)//2
partitionB = (m+n+1)//2 - partitionA
maxleftA = nums1[partitionA-1] if partitionA != 0 else float("-inf")
minrightA = nums1[partitionA] if partitionA != n else float("inf")
maxleftB = nums2[partitionB-1] if partitionB != 0 else float("-inf")
minrightB = nums2[partitionB] if partitionB != m else float("inf")
if maxleftA <= minrightB and maxleftB <= minrightB:
if (m+n)%2==0:
return (max(maxleftA,maxleftB) + min(minrightA,minrightB))/2
else:
return max(maxleftA,maxleftB)
elif maxleftA > minrightB:
right = partitionA-1
else:
left = partitionA + 1
print(median([1,2],[3,4]))