forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_num_A.py
More file actions
61 lines (42 loc) · 1.45 KB
/
count_num_A.py
File metadata and controls
61 lines (42 loc) · 1.45 KB
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
"""
Problem Statement
=================
Imagine you have a special keyboard with the following keys:
Key 1: Prints 'A' on screen
Key 2: (Ctrl-A): Select screen
Key 3: (Ctrl-C): Copy selection to buffer
Key 4: (Ctrl-V): Print buffer on screen appending it
after what has already been printed.
If you can only press the keyboard for N times (with the above four
keys), write a program to produce maximum numbers of A's. That is to
say, the input parameter is N (No. of keys that you can press), the
output is M (No. of As that you can produce).
Complexity
----------
* Recursive Solution : Exponential > O(2^n)
* Dynamic Programming: Quadratic O(n^2)
Reference
---------
* http://www.geeksforgeeks.org/how-to-print-maximum-number-of-a-using-given-four-keys/
"""
def count_a_recursive(n_times):
if n_times < 7:
return n_times
result = float("-inf")
for sub_prob in range(n_times - 3, 0, -1):
result = max(result, (n_times - sub_prob - 1) * count_a_recursive(sub_prob))
return result
def count_a(n_times):
if n_times < 7:
return n_times
T = [0 for _ in range(n_times + 1)]
for num in range(7):
T[num] = num
for n in range(7, n_times + 1):
for sub_prob in range(n - 3, 0, -1):
T[n] = max(T[n], T[sub_prob] * (n - sub_prob - 1))
return T[n_times]
if __name__ == '__main__':
expected = 9
assert expected == count_a_recursive(7)
assert expected == count_a(7)