[HackerRank][Python3] Pairs
2018. 7. 17. 00:08 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/pairs/problem
My Solution :
#!/usr/bin/env python3 def pairs(k, arr): found = 0 memo = {} for a in sorted(arr): if a-k in memo: found += 1 memo[a] = 1 return found n, k = map(int, input().split()) arr = list(map(int, input().rstrip().split())) result = pairs(k, arr) print(result)
My Solution2 :
#!/usr/bin/env python3 def pairs(k, arr): arr.sort() found = 0 i, j = 0, 1 while j < len(arr): if arr[j] - arr[i] == k: found += 1 j += 1 elif arr[j] - arr[i] < k: j += 1 else: i += 1 return found n, k = map(int, input().split()) arr = list(map(int, input().rstrip().split())) result = pairs(k, arr) print(result)
'프로그래밍 > HackerRank' 카테고리의 다른 글
[HackerRank][Python3] Abbreviation (0) | 2018.07.18 |
---|---|
[HackerRank][Python3] Max Array Sum (0) | 2018.07.18 |
[HackerRank][Python3] Making Candies (0) | 2018.07.18 |
[HackerRank][Python3] Minimum Time Required (0) | 2018.07.17 |
[HackerRank][Python3] Hash Tables: Ice Cream Parlor (0) | 2018.07.16 |
[HackerRank][Python3] Triple sum (0) | 2018.07.16 |
[HackerRank][Python3] Max Min (0) | 2018.07.16 |
[HackerRank][Python3] Greedy Florist (0) | 2018.07.16 |
최근에 달린 댓글 최근에 달린 댓글