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
'Programming > 1 Day 1 Commit' 카테고리의 다른 글
[LeetCode] Add Two Numbers (Python3) (0) | 2023.01.31 |
---|---|
[BaekJoon] 17413번: 단어뒤집기 2 (Python3) (0) | 2023.01.27 |
[LeetCode] Longest Common Prefix (Python3) (0) | 2023.01.25 |
[Programmers] 피자나눠먹기 (1) (Python3) (0) | 2023.01.24 |
[Programmers] 아이스아메리카노 (python3) (0) | 2023.01.20 |