일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- spring jpa 사이드프로젝트
- typescript 기초문법
- 유니티
- 파이썬 기초
- 유니티Material
- 괄호 회전하기 파이썬
- springboot 미니프로젝트
- 스프링부트 회원가입
- 타입스크립트 기본문법
- 프로그래머스 괄호 회전하기 python
- 스프링부트 미니프로젝트
- springboot 게시판만들기
- springboot 게시판
- 스프링부트 update
- spring jpa 게시판
- 스프링부트 게시판만들기
- 타입스크립트 기초
- JS기초
- 스프링부트 블로그
- springboot 사이드프로젝트
- 파이썬 괄호 회전하기
- python 괄호 회전하기
- springboot 게시판 프로젝트
- 스프링게시판프로젝트
- 유니티Cube
- 타입스크립트 기초문법
- 스프링 게시판 만들기
- jpa 게시판
- 스프링부트 블로그만들기
- 유니티기초
- Today
- Total
Digking's cave
HackerRank 해커랭크 ) Mini-Max Sum 본문
https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true
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.
Example
The minimum sum is and the maximum sum is . The function prints
16 24
Function Description
Complete the miniMaxSum function in the editor below.
miniMaxSum has the following parameter(s):
- arr: an array of integers
Print two space-separated integers on one line: the minimum sum and the maximum sum of of elements.
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
The numbers are , , , , and . Calculate the following sums using four of the five integers:
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
- Sum everything except , the sum is .
Hints: Beware of integer overflow! Use 64-bit Integer.
풀이
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static void miniMaxSum(List<Integer> arr) {
// Write your code here
arr.sort(Comparator.naturalOrder());
long result = 0;
for(int i = 0; i<arr.size() - 1; i++) {
result += arr.get(i);
}
System.out.print(result);
System.out.print(" ");
result = 0;
for(int i = arr.size() -1; i>0; i--) {
result += arr.get(i);
}
System.out.print(result);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Result.miniMaxSum(arr);
bufferedReader.close();
}
}
처음에는 해결했는데 test case 5개만 성공하고, 10개는 실패했다
이유는 총합 값의 범위가 int의 범위보다 넘을 수도 있어서였다!!
'코딩테스트 > 알고리즘' 카테고리의 다른 글
해커랭크 Hackerrank ) Migratory Birds (0) | 2023.05.27 |
---|---|
해커랭크 Hackerrank ) Angry Professor (0) | 2023.05.27 |
프로그래머스 ) 대소문자 바꿔서 출력하기(Java) (0) | 2023.05.16 |
프로그래머스 ) 숫자짝꿍 (Python) (0) | 2022.10.24 |
프로그래머스 ) n^2 배열 자르기[월간 코드 챌린지 시즌3] (Python) (0) | 2022.10.23 |