본문 바로가기
알고리즘/코드시그널

Codesignal - adjacent Elements Product

by 패쓰킴 2021. 4. 27.
728x90

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

 

Example.

For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.

7 and 3produce the largest product.

Input/Output

  • [execution time limit] 20 seconds (swift)
  • [input] array.integer inputArrayGuaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    -1000 ≤ inputArray[i] ≤ 1000.
  • An array of integers containing at least two elements.
  • [output] integer
    • The largest product of adjacent elements.

설명

배열의 앞뒤 값을 곱했을 때 가장 큰값을 찾아라

 

풀이

문제자체를 잘못이해해서 시간을 낭비 + 답을 보게됨

알고리즘 보다 영어 공부를 해야겠단 생각이 더 많아짐....

그래서 결론은! 그냥 문제 대로 배열의 원소 앞뒤 값을 곱했을 때 큰 값을 구하면 된다.

func adjacentElementsProduct(inputArray: [Int]) -> Int {
    var maxProduct = inputArray[0] * inputArray[1]

    for num in 2 ..< inputArray.count {
        maxProduct = max(maxProduct, inputArray[num - 1] * inputArray[num])
    }

    return maxProduct
}
728x90

'알고리즘 > 코드시그널' 카테고리의 다른 글

Codesignal - alphabeticShift  (0) 2021.04.29
Codesignal - almostIncreasingSequence  (0) 2021.04.29
Codesignal - allLongestString  (0) 2021.04.27
Codesignal - addBorder  (0) 2021.04.27
Codesignal - absoluteValuesSumMinimization  (0) 2021.04.26

댓글