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 .

題目解析

數(shù)組組合伦籍,一種方法是循環(huán)颅崩,另外一種是直接把所有的數(shù)字排序弯蚜,然后臨近的兩個(gè)相減

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()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末扮授,一起剝皮案震驚了整個(gè)濱河市免都,隨后出現(xiàn)的幾起案子贱枣,更是在濱河造成了極大的恐慌婉徘,老刑警劉巖捌议,帶你破解...
    沈念sama閱讀 211,743評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異嚼黔,居然都是意外死亡细层,警方通過(guò)查閱死者的電腦和手機(jī)惜辑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)疫赎,“玉大人韵丑,你說(shuō)我怎么就攤上這事⌒槎校” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,285評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵钓株,是天一觀的道長(zhǎng)实牡。 經(jīng)常有香客問(wèn)我,道長(zhǎng)轴合,這世上最難降的妖魔是什么创坞? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,485評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮受葛,結(jié)果婚禮上题涨,老公的妹妹穿的比我還像新娘。我一直安慰自己总滩,他們只是感情好纲堵,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著闰渔,像睡著了一般席函。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冈涧,一...
    開(kāi)封第一講書(shū)人閱讀 49,821評(píng)論 1 290
  • 那天茂附,我揣著相機(jī)與錄音,去河邊找鬼督弓。 笑死营曼,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的愚隧。 我是一名探鬼主播蒂阱,決...
    沈念sama閱讀 38,960評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼狂塘!你這毒婦竟也來(lái)了蒜危?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,719評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤睹耐,失蹤者是張志新(化名)和其女友劉穎辐赞,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體硝训,經(jīng)...
    沈念sama閱讀 44,186評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡响委,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評(píng)論 2 327
  • 正文 我和宋清朗相戀三年新思,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赘风。...
    茶點(diǎn)故事閱讀 38,650評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡夹囚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出邀窃,到底是詐尸還是另有隱情荸哟,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布瞬捕,位于F島的核電站鞍历,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏肪虎。R本人自食惡果不足惜劣砍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望扇救。 院中可真熱鬧刑枝,春花似錦、人聲如沸迅腔。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,757評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)沧烈。三九已至洁灵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間掺出,已是汗流浹背徽千。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,991評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留汤锨,地道東北人双抽。 一個(gè)月前我還...
    沈念sama閱讀 46,370評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像闲礼,于是被迫代替她去往敵國(guó)和親牍汹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,309評(píng)論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,448評(píng)論 0 13
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,435評(píng)論 0 23
  • 上周日下午兒子去安哲家玩柬泽,安哲媽媽特別熱情慎菲,帶著孩子們玩了很多運(yùn)動(dòng)項(xiàng)目,買(mǎi)了奶茶锨并,晚上還吃了烤鴨露该,吃完晚飯,已經(jīng)快...
    shangzhi閱讀 97評(píng)論 0 0
  • 年味是什么第煮,打開(kāi)電視不管哪個(gè)臺(tái)解幼,都在舉行晚會(huì)抑党。年味是什么,路過(guò)鄉(xiāng)村的小路撵摆,都能聞到臘肉的味道底靠。年味是什么,路旁的花...
    黃土不多百年太久閱讀 488評(píng)論 1 1