티스토리 뷰

Question

Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.


Example

  • For inputArray = [2, 4, 1, 0], the output should be arrayMaximalAdjacentDifference(inputArray) = 3.

Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] array.integer inputArray

    Guaranteed constraints: 3 ≤ inputArray.length ≤ 10, -15 ≤ inputArray[i] ≤ 15.

  • [output] integer

    • The maximal absolute difference.

MY_ANSWER

def arrayMaximalAdjacentDifference(inputArray):
    return max([abs(inputArray[i] - inputArray[i+1]) for i in range(len(inputArray)-1)])
  • 원소들간의 간격이 가장 큰 경우를 찾는 경우, abs과 max를 이용해 구할 수 있다.

Best_ANSWER

def arrayMaximalAdjacentDifference(a):
    diffs=[abs(a[i]-a[i+1]) for i in range(len(a)-1)]
    return max(diffs)
  • 본인의 답변과 다르게 과정을 나눈 모습, 코드를 보는 입장에서 좋은 코드이다.

 

'Codesignal' 카테고리의 다른 글

code signal - 22. avoidObstacles  (0) 2020.02.20
code signal - 21. isIPv4Address  (0) 2020.02.19
code signal - 19. areEquallyStrong  (0) 2020.02.19
code signal - 18. palindromeRearranging  (0) 2020.02.19
code signal - 17. arrayChange  (0) 2020.02.19
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함