새소식

Programming/1 Day 1 Commit

[HackerRank] Mini-Max Sum (Python3)

  • -

 

 

Mini-Max Sum | HackerRank

Find the maximum and minimum values obtained by summing four of five integers.

www.hackerrank.com

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
5개의 element를 가진 array를 받아서 그 중에서 4개를 더하는 데 그 더한 값 중 가장 큰 값과 가장 작은 값을 구하면 된다.
Sort를 한 다음에, 가장 앞의 값 (가장 작은 값) 을 빼고 더해서 -> Max
가장 뒤에 값 (가장 큰 값)을 빼고 더해서 -> Min을 구했다.

비교적 쉽게 풀렸던 문제. python에는 다양한 내장 함수들이 있어서 코딩할 때 참 편하다 ㅎㅎ

 

# Solution 1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def miniMaxSum(arr):
    # Write your code here
    arr = sorted(arr)
    min = 0
    max = 0
    for i in arr[1:]:
        max+=i
    for i in arr[:-1]:
        min+=i
    print(min, max)
    return min, max

if __name__ == '__main__':

    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)
 
 

GitHub - Park-Minjoo/CODINGINTERVIEW_PRACTICE: 1 Day 1 Problem since 2022.4.7

1 Day 1 Problem since 2022.4.7. Contribute to Park-Minjoo/CODINGINTERVIEW_PRACTICE development by creating an account on GitHub.

github.com

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.