문제 분석
- 자유자재로 로프를 사용하여, 들 수 있는 가장 높은 무게의 중량 수를 출력하라.
- 다만 분담하는 경우 모든 로프가 다 들 수 있는 무게여야 한다. → 내림차순 정렬
손으로 풀기
- N개의 로프를 내림차순으로 정렬한다.
- N개까지 순회하며 하나씩, 두개씩, 세개씩 로프의 수를 늘려가면서 들 수 있는 최대 중량을 result 배열에 넣는다(2중 for문)
- result 배열에서 가장 높은 수를 출력한다.
Sudo Code
함수_BOJ1127(로프개수 N, 로프가들 수있는 중량 1...N):
List<Integer> 로프배열;
List<Integer> 결과배열;
int 로프개수;
로프배열.sort(내림차순);
순서 = 1;
for(로프 순회):
결과배열.add(중량*순서);
순서++;
출력(결과배열에서가장높은수)
Code
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class BOJ1127 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> lopeArray = new ArrayList<>();
List<Integer> resultArr = new ArrayList<>();
int N = sc.nextInt();
for(int i=0;i<N;i++){
lopeArray.add(sc.nextInt());
}
lopeArray.sort((a,b) -> b-a);
int order = 1;
for(int i=0; i<N; i++){
resultArr.add(lopeArray.get(i) * order);
order++;
}
int maxResult = Collections.max(resultArr);
System.out.println(maxResult);
}
}