-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoPointer.py
324 lines (292 loc) · 8.98 KB
/
TwoPointer.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
# 1229. Meeting Scheduler
class Solution:
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
a1, a2 = 0, 0
n1, n2 = len(slots1), len(slots2)
slots1.sort()
slots2.sort()
while a1<n1 and a2<n2:
maxx = max(slots1[a1][0], slots2[a2][0])
miny = min(slots1[a1][1], slots2[a2][1])
if maxx>miny:
if slots1[a1][1]<slots2[a2][0]:
a1+=1
elif slots2[a2][1] < slots1[a1][0]:
a2+=1
else:
if miny-maxx>=duration:
# print(maxx, miny)
return [maxx, maxx+duration]
if slots1[a1][1]<slots2[a2][1]:
a1+=1
elif slots2[a2][1] < slots1[a1][1]:
a2+=1
return []
# 5. Longest Palindromic Substring
class Solution:
def longestPalindrome(self, s: str) -> str:
def helper(l, r):
while l >= 0 and r < n and s[l] == s[r]:
l -= 1
r += 1
return l + 1, r - 1
n = len(s)
tmp = 0
start = 0
end = 0
for i in range(n):
l1, r1 = helper(i, i)
if r1 - l1 + 1 > tmp:
tmp = r1 - l1 + 1
start = l1
end = r1
if i + 1 < n:
l2, r2 = helper(i, i + 1)
if r2 - l2 + 1 > tmp:
tmp = r2 - l2 + 1
start = l2
end = r2
# print(s[start: end + 1])
return s[start: end + 1]
# 11. Container With Most Water
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
maxarea = 0
while l < r:
width = r - l
maxarea = max(maxarea, min(height[l], height[r]) * width)
if height[l] <= height[r]:
l += 1
else:
r -= 1
return maxarea
# 1658. Minimum Operations to Reduce X to Zero
class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
total = sum(nums)
n = len(nums)
maxi = -1
left = 0
current = 0
for right in range(n):
current += nums[right]
while current > total - x and left <= right:
current -= nums[left]
left += 1
if current == total - x:
maxi = max(maxi, right - left + 1)
return n - maxi if maxi != -1 else -1
class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
current = sum(nums)
n = len(nums)
mini = inf
left = 0
for right in range(n):
# sum([0,..,left) + (right,...,n-1]) = x
current -= nums[right]
# if smaller, move `left` to left
while current < x and left <= right:
current += nums[left]
left += 1
# check if equal
if current == x:
mini = min(mini, (n-1-right)+left)
return mini if mini != inf else -1
# 88. Merge Sorted Array
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
p = m + n
m -= 1
n -= 1
while p > 0:
p -= 1
if m < 0:
nums1[p] = nums2[n]
n -= 1
elif n < 0:
nums1[p] = nums1[m]
m -= 1
elif nums1[m] < nums2[n]:
nums1[p] = nums2[n]
n -= 1
else:
nums1[p] = nums1[m]
m -= 1
# print(nums1, m, n)
# 283. Move Zeroes
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
pre = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[i], nums[pre] = nums[pre], nums[i]
pre += 1
# 15. 3Sum
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
ans = []
n = len(nums)
nums.sort()
def twoSum(i):
l, r = i + 1, n - 1
while l < r:
s = nums[i] + nums[l] + nums[r]
if s == 0:
ans.append([nums[i], nums[l], nums[r]])
l += 1
r -= 1
while l < r and nums[l] == nums[l - 1]:
l += 1
elif s > 0:
r -= 1
else:
l += 1
for i in range(n):
if nums[i] > 0:
break
if i == 0 or nums[i - 1] != nums[i]:
twoSum(i)
return ans
# 1868. Product of Two Run-Length Encoded Arrays
class Solution:
def findRLEArray(self, encoded1: List[List[int]], encoded2: List[List[int]]) -> List[List[int]]:
n1 = len(encoded1)
n2 = len(encoded2)
i1 = 0
i2 = 0
ans = []
while i1 < n1 and i2 < n2:
num1 = encoded1[i1][0]
num2 = encoded2[i2][0]
v = num1 * num2
cnt1 = encoded1[i1][1]
cnt2 = encoded2[i2][1]
f = 0
if cnt1 < cnt2:
f = cnt1
encoded2[i2][1] -= cnt1
i1 += 1
elif cnt1 > cnt2:
f = cnt2
encoded1[i1][1] -= cnt2
i2 += 1
else:
f = cnt1
encoded1[i1][1] -= cnt2
encoded2[i2][1] -= cnt1
i1 += 1
i2 += 1
if ans and ans[-1][0] == v:
ans[-1][1] += f
else:
ans.append([v, f])
return ans
# 31. Next Permutation
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
i = n-2;
while i>=0 and nums[i]>=nums[i+1]:
i-=1
if i==-1:
nums.reverse()
return
j = n-1
while nums[j]<=nums[i]:
j-=1
nums[i], nums[j] = nums[j], nums[i]
l, r = i+1, n-1
while l<r:
nums[l], nums[r] = nums[r], nums[l]
l+=1
r-=1
# 443. String Compression
class Solution:
def compress(self, chars: List[str]) -> int:
n = len(chars)
if n == 1:
return n
cnt = 1
ans = 0
start = 0
for i in range(1, n):
if chars[i - 1] == chars[i]:
cnt += 1
else:
if cnt == 1:
ans += 1
chars[start] = chars[i - 1]
start += 1
else:
ccnt = 0
chars[start] = chars[i - 1]
q = []
while cnt:
q.append(str(cnt % 10))
cnt //= 10
ccnt += 1
while q:
# print(q)
start += 1
chars[start] = q.pop(-1)
start += 1
ans += 1 + ccnt
cnt = 1
if cnt == 1:
ans += 1
chars[start] = chars[i]
start += 1
else:
ccnt = 0
chars[start] = chars[i]
q = []
while cnt:
q.append(str(cnt % 10))
cnt //= 10
ccnt += 1
while q:
# print(q)
start += 1
chars[start] = q.pop(-1)
start += 1
ans += 1 + ccnt
return ans
# 1868. Product of Two Run-Length Encoded Arrays
class Solution:
def findRLEArray(self, encoded1: List[List[int]], encoded2: List[List[int]]) -> List[List[int]]:
# two pointers
p1, p2 = 0, 0
ans = []
while p1 < len(encoded1) and p2 < len(encoded2):
l1 = encoded1[p1][1]
l2 = encoded2[p2][1]
current_v = encoded1[p1][0] * encoded2[p2][0]
if l1 < l2:
current_f = l1
p1 += 1
encoded2[p2][1] = l2 - l1 # update frequency for uncounted encoded2
elif l2 < l1:
current_f = l2
p2 += 1
encoded1[p1][1] = l1 - l2 # update frequency for uncounted encoded1
else: # l1 == l2
current_f = l2
p1 += 1
p2 += 1
# update ans
if ans and ans[-1][0] == current_v: # check if current_v == previous value in ans
ans[-1][1] += current_f
else:
ans.append([current_v, current_f])
return ans