HackerRank:Minimum Absolute Difference in an Array Python3

题目

题目链接: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()

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,129评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,395评论 0 13
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,630评论 0 23
  • 上周日下午儿子去安哲家玩,安哲妈妈特别热情,带着孩子们玩了很多运动项目,买了奶茶,晚上还吃了烤鸭,吃完晚饭,已经快...
    shangzhi阅读 84评论 0 0
  • 年味是什么,打开电视不管哪个台,都在举行晚会。年味是什么,路过乡村的小路,都能闻到腊肉的味道。年味是什么,路旁的花...
    黄土不多百年太久阅读 473评论 1 1