나는 이렇게 학습한다/Algorithm & SQL

1211. Maximum Product

daco2020 2022. 12. 11. 23:04
반응형

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:]))


반응형