-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest_triple_products.py
96 lines (75 loc) · 2.63 KB
/
largest_triple_products.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
You're given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer than three elements).
Note that the three largest elements used to form any product may have the same values as one another, but they must be at different indices in arr.
Signature
int[] findMaxProduct(int[] arr)
Input
n is in the range [1, 100,000].
Each value arr[i] is in the range [1, 1,000].
Output
Return a list of n integers output[0..(n-1)], as described above.
Example 1
n = 5
arr = [1, 2, 3, 4, 5]
output = [-1, -1, 6, 24, 60]
The 3rd element of output is 3*2*1 = 6, the 4th is 4*3*2 = 24, and the 5th is 5*4*3 = 60.
Example 2
n = 5
arr = [2, 1, 2, 1, 2]
output = [-1, -1, 4, 4, 8]
The 3rd element of output is 2*2*1 = 4, the 4th is 2*2*1 = 4, and the 5th is 2*2*2 = 8.
import math
# Add any extra import statements you may need here
from heapq import heappush, heappop, nlargest
from numpy import product
# Add any helper functions you may need here
def findMaxProduct(arr):
# Write your code here
res = [-1, -1, product(arr[:3])]
h = arr[:3]
for i in range(3,len(arr)):
heappush(h, arr[i])
res.append(product(nlargest(3, h)))
return res
# These are the tests we use to determine if the solution is correct.
# You can add your own at the bottom.
def printInteger(n):
print('[', n, ']', sep='', end='')
def printIntegerList(array):
size = len(array)
print('[', end='')
for i in range(size):
if i != 0:
print(', ', end='')
print(array[i], end='')
print(']', end='')
test_case_number = 1
def check(expected, output):
global test_case_number
expected_size = len(expected)
output_size = len(output)
result = True
if expected_size != output_size:
result = False
for i in range(min(expected_size, output_size)):
result &= (output[i] == expected[i])
rightTick = '\u2713'
wrongTick = '\u2717'
if result:
print(rightTick, 'Test #', test_case_number, sep='')
else:
print(wrongTick, 'Test #', test_case_number, ': Expected ', sep='', end='')
printIntegerList(expected)
print(' Your output: ', end='')
printIntegerList(output)
print()
test_case_number += 1
if __name__ == "__main__":
arr_1 = [1, 2, 3, 4, 5]
expected_1 = [-1, -1, 6, 24, 60]
output_1 = findMaxProduct(arr_1)
check(expected_1, output_1)
arr_2 = [2, 4, 7, 1, 5, 3]
expected_2 = [-1, -1, 56, 56, 140, 140]
output_2 = findMaxProduct(arr_2)
check(expected_2, output_2)
# Add your own test cases here