-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxsub.py
329 lines (273 loc) · 8.88 KB
/
maxsub.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
import sys
import math
def get_max_norep_sub(sent):
if len(sent) < 1:
return ""
# curindex is the last index of the current substr
curindex = maxindex = 0
curlen = maxlen = 1
for curindex in range(1, len(sent)):
# check if the next char repeated in the current substr
repeatindex = sent.find(sent[curindex], curindex - curlen, curindex)
if repeatindex < 0:
curlen += 1
if curlen > maxlen:
maxlen = curlen
maxindex = curindex
# print ("(%d %d)" % (maxlen, maxindex))
else:
curlen = curindex - repeatindex
return sent[maxindex - maxlen + 1:maxindex + 1]
def test_maxsub():
while (True):
content = input("Please input the string to find out the substring without repeated character:\n")
if content.lower() == "quit":
sys.exit()
print("The substring is:" + get_max_norep_sub(content))
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self._capacity = capacity
self._currentnum = 0
self._head = None
self._tail = None
self._keymap = {}
def get(self, key):
"""
:rtype: int
"""
findpair = self._keymap.get(key)
if findpair is None:
return -1
else:
self._movetohead(findpair)
return findpair[0]
def set(self, key, value):
"""
:type key: int
:type value: int
:rtype: nothing
"""
findpair = self._keymap.get(key)
if findpair is None:
pairtoadd = [value, None, None]
self._keymap[key] = pairtoadd
self._addtohead(pairtoadd)
else:
findpair[0] = value
self._movetohead(findpair)
def _addtohead(self, pairtoadd):
if self._head is not None:
pairtoadd[2] = self._head
self._head[1] = pairtoadd
self._head = pairtoadd
if self._tail is None:
self._tail = pairtoadd
if self._currentnum == self._capacity:
self._keymap.pop(self._tail[0])
self._tail = self._tail[1]
else:
self._currentnum += 1
def _movetohead(self, findpair):
if findpair is self._head:
return
if findpair is self._tail:
self._tail = self._tail[1]
#pick up from the linked list
prvpair = findpair[1]
nxtpair = findpair[2]
if prvpair is not None:
prvpair[2] = nxtpair
if nxtpair is not None:
nxtpair[1] = prvpair
#put to the first of the link
findpair[2] = self._head
findpair[1] = None
self._head[1] = findpair
self._head = findpair
def testprint(self):
print(self._keymap)
print(self._head)
print(self._tail)
print("================")
def test_lru():
cache = LRUCache(10)
cache.set(10, 20)
cache.set(11, 22)
cache.set(12, 24)
cache.testprint()
cache.set(13,26)
cache.get(10)
cache.testprint()
class Solution(object):
def maximumGap(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
num_len = len(nums)
if num_len <= 1:
return 0;
num_max = max(nums)
num_min = min(nums)
if num_len == 2:
return num_max - num_min
bulk_size = (num_max - num_min) / (num_len - 1)
if bulk_size == 0:
return 0
bulks = []
bulk_min = num_min
for index in range(num_len - 1):
bulks.append([bulk_min + bulk_size, bulk_min])
bulk_min += bulk_size
bulks.append([num_max, num_max]) # for safty, avoid index overflow
for num in nums:
bulk_index = math.floor((num - num_min) / bulk_size)
print("bulk_index:%d" % (bulk_index))
if num > bulks[bulk_index][1]:
bulks[bulk_index][1] = num
if num < bulks[bulk_index][0]:
bulks[bulk_index][0] = num
# max_gap should span the bulks
max_gap = cur_max_gap = 0
prev_bulk_max = num_max
for bulk in bulks:
if bulk[0] > bulk[1]: # empty bulk
pass
else:
cur_max_gap = bulk[0] - prev_bulk_max
tmp = prev_bulk_max
prev_bulk_max = bulk[1]
if cur_max_gap > max_gap:
max_gap = cur_max_gap
gap_first = tmp
gap_last = bulk[0]
print("(%d, %d)" % (gap_first, gap_last))
return max_gap
def letterCombinations(self, digits):
dletters = {'0':[' '],
'1':['*'],
'2':['a', 'b', 'c'],
'3':['d', 'e', 'f'],
'4':['g', 'h', 'i'],
'5':['j', 'k', 'l'],
'6':['m', 'n', 'o'],
'7':['p', 'q', 'r', 's'],
'8':['t', 'u', 'v'],
'9':['w', 'x', 'y', 'z']
}
dsize = {'0':1, '1':1, '2':3, '3':3, '4':3, '5':3, '6':3, '7':4, '8':3, '9':4}
num = 1
for d in digits:
num *= dsize[d]
res = []
for i in range(num):
str = ""
val = i
for d in digits:
(val, index) = divmod(val, dsize[d])
str += dletters[d][index]
res.append(str)
return res
def maxProfit2(self, prices):
if len(prices) ==0:
return 0
cur_pro = 0
cur_low = prices[0]
cur_high = prices[0]
for price in prices:
if price >= cur_high:
cur_high = price
else:
cur_pro += (cur_high - cur_low)
cur_low = cur_high = price
if price < cur_low:
cur_low = price
cur_pro += (cur_high - cur_low)
return cur_pro
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if len(prices)==0:
return 0
max_pro = 0
buy_pri = prices[0]
sell_pro = 0
for price in prices:
if price < buy_pri:
buy_pri = price
else:
sell_pro = price - buy_pri
if sell_pro > max_pro:
max_pro = sell_pro
return max_pro
def findMaximumXOR(self, nums):
answer = 0
for i in range(32)[::-1]:
answer <<= 1
prefixes = {num >> i for num in nums}
answer += any(answer ^ 1 ^ p in prefixes for p in prefixes)
return answer
def mergeSort(arr, left, right):
if right > left:
mid = left + (right - left) // 2
if mid > left:
mergeSort(arr, left, mid)
if right > mid + 1:
mergeSort(arr, mid+1, right)
tmp_arr = []
left_iter = left
right_iter = mid + 1
for i in range(left, right +1):
if right_iter > right or (arr[left_iter] <= arr[right_iter] and left_iter <= mid):
tmp_arr.append(arr[left_iter])
left_iter += 1
else:
tmp_arr.append(arr[right_iter])
right_iter += 1
print("--------------------------")
print(arr[left:right+1])
print(tmp_arr)
print("============================")
for i in range(left, right +1):
arr[i] = tmp_arr[i-left]
def split_2_arr(arr1, arr2):
len1 = len(arr1)
len2 = len(arr2)
if len1 < len2:
start_arr = arr2
follow_arr = arr1
else:
start_arr = arr1
follow_arr = arr1
if len(follow_arr) == 0:
print(start_arr)
return
magn = len(start_arr) // len(follow_arr)
remain = len(start_arr) % len(follow_arr)
outarr = []
start_iter = 0
for i in range(len(follow_arr)):
for j in range(magn):
outarr.append(start_arr[start_iter])
start_iter += 1
if remain > 0:
outarr.append(start_arr[start_iter])
start_iter += 1
remain -= 1
outarr.append(follow_arr[i])
print("".join(outarr))
if __name__ == "__main__":
testarr = [15252, 16764, 27963, 7817, 26155, 20757, 3478, 22602, 20404, 6739, 16790, 10588, 16521, 6644, 20880,
15632, 27078, 25463, 20124, 15728, 30042, 16604, 17223, 4388, 23646, 32683, 23688, 12439, 30630, 3895,
7926, 22101, 32406, 21540, 31799, 3768, 26679, 21799, 23740]
sol = Solution()
print (sol.maxProfit3([1,2,4,2,5,7,2,4,9,0]))
#test_lru()
#print(sol.maximumGap(testarr))
# print(sol.maximumGap([3,6,9,1]))
# print(sol.letterCombinations("12345"))