규도자 개발 블로그

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Mini-Max Sum 본문

알고리즘/풀이

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Mini-Max Sum

규도자 (gyudoza) 2019. 3. 9. 15:32

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.

For example, . Our minimum sum is  and our maximum sum is . We would print

16 24

Function Description

Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Input Format

A single line of five space-separated integers.

Constraints

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

Our initial numbers are , and . We can calculate the following sums using four of the five integers:

  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. If we sum everything except , our sum is .

Hints: Beware of integer overflow! Use 64-bit Integer.


Need help to get started? Try the Solve Me First problem

풀이

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the miniMaxSum function below.
def miniMaxSum(arr):
    arr.sort()
    print(sum(arr[:4]), sum(arr[1:]))

if __name__ == '__main__':
    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)

설명

5개의 숫자가 입력돼있는 배열을 받은 뒤에 그 중 4개의 숫자를 조합한다. 좌측에는 4개의 숫자를 조합하여 만들 수 있는 가장 작은 수를 출력하고 우측에는 4개의 숫자를 조합하여 만들 수 있는 가장 큰 수를 출력한다.

 어차피 가장 작은 수는 가장 작은 수들끼리 더했을 때 나오고, 가장 큰 수는 가장 큰 수들끼리 더했을 때 나오므로 오름차순으로 정렬해서 좌측에는 0~3번 인덱스까지의 숫자를 더한 값, 우측에는 2~4번까지의 인덱스를 더한 값을 출력하여 간단하게 해결하였다.


Comments