/
DCP_61-70.py
377 lines (316 loc) · 12.3 KB
/
DCP_61-70.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/python
# coding=utf-8
'''
__author__ = 'sunp'
__date__ = '2019/1/24'
'''
import unittest, random
from collections import defaultdict, OrderedDict
class Solution1(object):
'''Google
Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y.
Do this faster than the naive method of repeated multiplication.
For example, pow(2, 10) should return 1024.
'''
def pow(self, x, y):
# divide and conquer
if y == 0:
return 1
if y == -(1 << 31):
y >>= 1
x *= x
if y < 0:
y = -y
x = 1 / x
half = self.pow(x * x, y >> 1)
return half * x if y & 1 else half
class Solution2(object):
'''Facebook
There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.
Given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
- Right, then down
- Down, then right
Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.
'''
def ways(self, M, N):
# dp
dp = [[1 for _ in range(N)] for _ in range(M)]
for i in range(1, M):
for j in range(1, N):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[-1][-1]
class Solution3(object):
'''Microsoft*
Given a 2D matrix of characters and a target word, write a function that returns whether the word can be found in the matrix by going left-to-right, or up-to-down.
Given the following matrix:
[['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']]
and the target word 'FOAM', you should return true, since it's the leftmost column. Similarly, given the target word 'MASS', you should return true, since it's the last row.
'''
def search(self, matrix, word):
# backtracking
self.matrix = matrix
self.word = word
self.m = len(matrix)
self.n = len(matrix[0])
for i in range(self.m):
for j in range(self.n):
if self._dfs(i, j, 0, self.matrix[i][j]):
return True
return False
def _dfs(self, i, j, k, tmp):
if self.word[k] != tmp[k]:
return False
if k == len(self.word) - 1:
return True
return ((i < self.m - 1 and self._dfs(i+1, j, k+1, tmp+self.matrix[i+1][j])) or
(j < self.n - 1 and self._dfs(i, j+1, k+1, tmp+self.matrix[i][j+1])))
class Solution4(object):
'''Google
A knight's tour is a sequence of moves by a knight on a chessboard such that all squares are visited once.
Given N, write a function to return the number of knight's tours on an N by N chessboard.
'''
pass
class Solution5(object):
'''Amazon*
Given a N by M matrix of numbers, print out the matrix in a clockwise spiral.
For example, given the following matrix:
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
You should print out the following:
[1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12]
'''
def spiral1(self, matrix):
# brute
res = []
row_beg, col_beg, row_end, col_end = 0, 0, len(matrix)-1, len(matrix[0])-1
while row_beg < row_end and col_beg < col_end:
for j in range(col_beg, col_end+1):
res.append(matrix[row_beg][j])
row_beg += 1
for i in range(row_beg, row_end+1):
res.append(matrix[i][col_end])
col_end -= 1
for j in range(col_end, col_beg-1, -1):
res.append(matrix[row_end][j])
row_end -= 1
for i in range(row_end, row_beg-1, -1):
res.append(matrix[i][col_beg])
col_beg += 1
return res
def spiral2(self, matrix):
# transpose
if not matrix:
return []
row = list(matrix.pop(0))
t = list(zip(*matrix))[::-1]
return row + self.spiral2(t)
class Solution6(object):
'''Square*
Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin.
Write a function to simulate an unbiased coin toss.
'''
def toss_unbiased1(self):
# P(01) = P(10) = p*(1-p)
while True:
x = self._toss_biased()
y = self._toss_biased()
if x == 0 and y == 1:
return 0
if x == 1 and y == 0:
return 1
def toss_unbiased2(self):
# frequency => probability
n_experiments = 100000
res_biased = {1: 0, 0: 0}
res_unbiased = {1: 0, 0: 0}
for i in range(n_experiments):
coin = self._toss_biased()
res_biased[coin] += 1
coin = not coin if i & 1 else coin
res_unbiased[coin] += 1
assert round(res_biased[0] / n_experiments, 2) == 0.7
assert round(res_biased[1] / n_experiments, 2) == 0.3
assert round(res_unbiased[0] / n_experiments, 2) == 0.5
assert round(res_unbiased[1] / n_experiments, 2) == 0.5
def _toss_biased(self):
return 0 if random.random() < 0.7 else 1
class Solution7(object):
'''Google*
Implement an LFU (Least Frequently Used) cache. It should be able to be initialized with a cache size n, and contain the following methods:
- set(key, value): sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least frequently used item. If there is a tie, then the least recently used key should be removed.
- get(key): gets the value at key. If no such key exists, return null.
Each operation should run in O(1) time.
'''
def __init__(self, capacity):
self.remain = capacity
self.min_freq = 1
self.d_key = {}
self.d_freq = defaultdict(OrderedDict)
def get(self, key):
if key not in self.d_key:
return None
self._update(key)
return self.d_key[key][0]
def set(self, key, value):
if key in self.d_key:
self._update(key, value)
else:
self.d_key[key] = (value, 1)
self.d_freq[1][key] = (value, 1)
if self.remain == 0:
removed = self.d_freq[self.min_freq].popitem(last=False)
del self.d_key[removed[0]]
else:
self.remain -= 1
self.min_freq = 1
def _update(self, key, new_val=None):
value, freq = self.d_key[key]
if new_val:
value = new_val
del self.d_freq[freq][key]
if len(self.d_freq[self.min_freq]) == 0:
self.min_freq += 1
self.d_key[key] = (value, freq+1)
self.d_freq[freq+1][key] = (value, freq+1)
class Solution8(object):
'''Google
On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces.
You are given N bishops, represented as (row, column) tuples on a M by M chessboard. Write a function to count the number of pairs of bishops that attack each other. The ordering of the pair doesn't matter: (1, 2) is considered the same as (2, 1).
For example, given M = 5 and the list of bishops:
(0, 0)
(1, 2)
(2, 2)
(4, 0)
The board would look like this:
[b 0 0 0 0]
[0 0 b 0 0]
[0 0 b 0 0]
[0 0 0 0 0]
[b 0 0 0 0]
You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3 and 4.
'''
def attacks(self, M, bishops):
# traverse diagonals
res = 0
for i in range(M):
res += self._backslash(bishops, M, i, 0)
res += self._slash(bishops, M, i, 0)
if i != 0:
res += self._backslash(bishops, M, 0, i)
res += self._slash(bishops, M, M-1, i)
return res
def _backslash(self, bishops, M, i, j):
count = 0
while i < M and j < M:
if (i, j) in bishops:
count += 1
i, j = i+1, j+1
return (count - 1) * count / 2
def _slash(self, bishops, M, i, j):
count = 0
while i >= 0 and j < M:
if (i, j) in bishops:
count += 1
i, j = i-1, j+1
return (count - 1) * count / 2
class Solution9(object):
'''Facebook
Given a list of integers, return the largest product that can be made by multiplying any three integers.
For example, if the list is [-10, -10, 5, 2], we should return 500, since that's -10 * -10 * 5.
You can assume the list has at least three integers.
'''
def max1(self, nums):
# backtracking
self.res = nums[0]*nums[1]*nums[2]
self._dfs(nums, 0, [])
return self.res
def _dfs(self, nums, pos, tmp):
if len(tmp) == 3:
self.res = max(self.res, tmp[0]*tmp[1]*tmp[2])
return
for i in range(pos, len(nums)):
self._dfs(nums, i+1, tmp+[nums[i]])
def max2(self, nums):
# dp
res = nums[0]*nums[1]*nums[2]
two_max = two_min = nums[0]*nums[1]
for i in range(2, len(nums)):
res = max(res, two_max*nums[i], two_min*nums[i])
for j in range(i):
two_max = max(two_max, nums[j]*nums[i])
two_min = min(two_min, nums[j]*nums[i])
return res
class Solution10(object):
'''Microsoft
A number is considered perfect if its digits sum up to exactly 10.
Given a positive integer n, return the n-th perfect number.
For example, given 1, you should return 19. Given 2, you should return 28.
'''
def perfect(self, num):
tmp = 0
for char in str(num):
tmp += int(char)
return num*10 + (10-tmp)
class TestSolutions(unittest.TestCase):
def test_solution1(self):
sol = Solution1()
self.assertEqual(sol.pow(2, 10), 1024)
self.assertEqual(sol.pow(2, -2), 0.25)
self.assertAlmostEqual(sol.pow(2.1, 3), 9.261)
def test_solution2(self):
sol = Solution2()
self.assertEqual(sol.ways(1, 1), 1)
self.assertEqual(sol.ways(2, 2), 2)
self.assertEqual(sol.ways(5, 5), 70)
def test_solution3(self):
sol = Solution3()
arg1 = [['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']]
arg21 = 'FOAM'
arg22 = 'MASS'
arg23 = 'FBOS'
self.assertTrue(sol.search(arg1, arg21))
self.assertTrue(sol.search(arg1, arg22))
self.assertFalse(sol.search(arg1, arg23))
def test_solution5(self):
sol = Solution5()
arg = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
res = [1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12]
self.assertEqual(sol.spiral1(arg), res)
self.assertEqual(arg, [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
self.assertEqual(sol.spiral2(arg), res)
self.assertEqual(arg, [[6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
def test_solution6(self):
sol = Solution6()
sol.toss_unbiased2()
def test_solution8(self):
sol = Solution8()
arg1 = 5
arg21 = [(0, 0), (1, 2), (2, 2), (4, 0)]
arg22 = [(0, 0), (1, 2), (2, 2)]
arg23 = [(0, 0), (1, 2), (2, 2), (4, 0), (4, 4)]
self.assertEqual(sol.attacks(arg1, arg21), 2)
self.assertEqual(sol.attacks(arg1, arg22), 1)
self.assertEqual(sol.attacks(arg1, arg23), 4)
def test_solution9(self):
sol = Solution9()
arg = [-10, -10, 5, 2, 3, 4]
self.assertEqual(sol.max1(arg), 500)
self.assertEqual(sol.max2(arg), 500)
def test_solution10(self):
sol = Solution10()
self.assertEqual(sol.perfect(1), 19)
self.assertEqual(sol.perfect(24), 244)
self.assertEqual(sol.perfect(19), 190)
if __name__ == '__main__':
unittest.main()