美文网首页
HackerRank:Minimum Absolute Diff

HackerRank:Minimum Absolute Diff

作者: 流浪山人 | 来源:发表于2019-12-12 17:12 被阅读0次

    题目

    题目链接:https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms

    Consider an array of integers, . We define the absolute difference between two elements, and (where ), to be the absolute value of .

    Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array we can create pairs of numbers: and . The absolute differences for these pairs are , and . The minimum absolute difference is .

    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 , the size of .
    The second line contains space-separated integers .

    Constraints

    Output Format

    Print the minimum absolute difference between any two elements in the array.

    Sample Input 0

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">3
    3 -7 0
    </pre>

    Sample Output 0

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">3
    </pre>

    Explanation 0

    With integers in our array, we have three possible pairs: , , and . The absolute values of the differences between these pairs are as follows:

    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 .

    Sample Input 1

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">10
    -59 -36 -13 1 -53 -92 -2 -96 -54 75
    </pre>

    Sample Output 1

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">1
    </pre>

    Explanation 1

    The smallest absolute difference is .

    Sample Input 2

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">5
    1 -3 71 68 17
    </pre>

    Sample Output 2

    <pre style="border: none; font-style: inherit; font-variant: inherit; font-weight: 400; font-stretch: inherit; line-height: 20px; font-family: var(--font-family-input); font-size: 14px; margin: 12px 0px 0px; outline: 0px; padding: 20px; vertical-align: baseline; color: var(--color-text-dark); border-radius: 2px; display: block; word-break: break-word; overflow-wrap: break-word; white-space: pre-wrap; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: var(--color-bg); overflow-x: auto;">3
    </pre>

    Explanation 2

    The minimum absolute difference is .

    题目解析

    数组组合,一种方法是循环,另外一种是直接把所有的数字排序,然后临近的两个相减

    ANSWER

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    # Complete the minimumAbsoluteDifference function below.
    def minimumAbsoluteDifference(arr):
        arr.sort()
    
        mindata=abs(arr[0]-arr[1])
    
        for i in range(len(arr)-1):        
                mindata=min(mindata,abs(arr[i]-arr[i+1]))
        return (mindata)
    
    
    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()
    
    

    相关文章

      网友评论

          本文标题:HackerRank:Minimum Absolute Diff

          本文链接:https://www.haomeiwen.com/subject/vdmtnctx.html