반응형
Task
Given an array of integers , Find the maximum product obtained from multiplying 2 adjacent numbers in the array.
Notes
Array/list size is at least 2.
Array/list numbers could be a mixture of positives, negatives also zeroes .
Input >> Output Examples
adjacentElementsProduct([1, 2, 3]); ==> return 6
Explanation:
The maximum product obtained from multiplying 2 * 3 = 6, and they're adjacent numbers in the array.
adjacentElementsProduct([9, 5, 10, 2, 24, -1, -48]); ==> return 50
Explanation:
Max product obtained from multiplying 5 * 10 = 50 .
adjacentElementsProduct([-23, 4, -5, 99, -27, 329, -2, 7, -921]) ==> return -14
Explanation:
The maximum product obtained from multiplying -2 * 7 = -14, and they're adjacent numbers in the array.
Solution:
def adjacent_element_product(array):
return max(array[i]*array[i+1] for i, v in enumerate(array) if len(array[i:i+2]) == 2)
def adjacent_element_product(array):
return max(array[i]*array[i+1] for i in range(len(array)-1))
def adjacent_element_product(array):
return max(x * y for x, y in zip(array, array[1:]))
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1213. Exclamation marks series #1: Remove an exclamation mark from the end of string (0) | 2022.12.13 |
---|---|
1212. String cleaning (0) | 2022.12.12 |
1210. Bumps in the Road (0) | 2022.12.11 |
1209. Grasshopper - Combine strings (0) | 2022.12.09 |
1208. A Strange Trip to the Market (0) | 2022.12.08 |