일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Baekjoon
- 프로그래머스 파이썬
- PYTHON
- 편스토랑 우승상품
- 백준
- programmers
- Docker
- SW Expert Academy
- 맥북
- gs25
- 캐치카페
- hackerrank
- Real or Not? NLP with Disaster Tweets
- 편스토랑
- 코로나19
- ChatGPT
- Git
- ubuntu
- 파이썬
- github
- AI 경진대회
- 더현대서울 맛집
- 자연어처리
- Kaggle
- 우분투
- leetcode
- 데이콘
- 금융문자분석경진대회
- dacon
- 프로그래머스
- Today
- Total
솜씨좋은장씨
[HackerRank] Minimum Absolute Difference in an Array (Python) 본문
[HackerRank] Minimum Absolute Difference in an Array (Python)
솜씨좋은장씨 2020. 4. 7. 00:21
Consider an array of integers, arr = [ arr [0], arr [1], ... , arr [n - 1]]. We define the absolute difference between two elements, a[i] and a[j] (where i != j), to be the absolute value of a[i] - a[j].
Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array arr = [ -2, 2, 4 ] we can create 3 pairs of numbers: [-2, 2], [-2, 4] and [2, 4]. The absolute differences for these pairs are |(-2) -2| = 4, |(-2) - 4| = 6 and |2-4| = 2. The minimum absolute difference is 2.
Function Description
Complete the minimumAbsoluteDifference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements.
minimumAbsoluteDifference has the following parameter(s):
- n: an integer that represents the length of arr
- arr: an array of integers
Input Format
The first line contains a single integer n, the size of arr.
The second line contains n space-separated integers arr [i].
Constraints
- 2 <= n <= 10^5
- -10^9 <= arr [i] <= 10^9
Output Format
Print the minimum absolute difference between any two elements in the array.
Sample Input 0
3
3 -7 0
Sample Output 0
3
Explanation 0
With n = 3 integers in our array, we have three possible pairs: (3, -7), (3, 0), and (-7, 0). The absolute values of the differences between these pairs are as follows:
- |3- -7| => 10
- |3 - 0| => 3
- |-7 -0| => 7
Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible absolute differences is 3.
Sample Input 1
10
-59 -36 -13 1 -53 -92 -2 -96 -54 75
Sample Output 1
1
Explanation 1
The smallest absolute difference is |-54- -53| = 1.
Sample Input 2
5
1 -3 71 68 17
Sample Output 2
3
Explanation 2
The minimum absolute difference is |71 - 68| = 3.
첫번째 시도
#!/bin/python3
import math
import os
import random
import re
import sys
import itertools
# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
my_permutations = list(set(itertools.permutations(arr, 2)))
arr_list = [abs(permu[0] - permu[1]) for permu in my_permutations]
return min(arr_list)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = minimumAbsoluteDifference(arr)
fptr.write(str(result) + '\n')
fptr.close()
결과
두번째 시도
#!/bin/python3
import math
import os
import random
import re
import sys
import itertools
# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
sorted_arr = sorted(arr)
diff = abs(sorted_arr[0] - sorted_arr[1])
for i in range(1, len(sorted_arr)-1):
new_diff = abs(sorted_arr[i] - sorted_arr[i+1])
if diff > new_diff:
diff = new_diff
return diff
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = minimumAbsoluteDifference(arr)
fptr.write(str(result) + '\n')
fptr.close()
결과
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[Programmers] 힙 : 디스크 컨트롤러 (Python) (0) | 2020.04.09 |
---|---|
[BaekJoon] 9465번 : 스티커 (Python) (1) | 2020.04.08 |
[Programmers] 힙 : 더 맵게 (Python) (0) | 2020.04.06 |
[HackerRank] Time Conversion (Python) (0) | 2020.04.05 |
[HackerRank] Birthday Cake Candles (Python) (0) | 2020.04.05 |