Coding/백준

2217번 로프

labote 2021. 5. 3. 10:50

 그리디 알고리즘

 

 내림차순으로 정렬한 뒤에 본인 위치를 포함해 앞의 개수를 본인값과 곱해주면 된다.

 

 ex) 10 9 8

 -> 10 * 1

 -> 9 * 2

 -> 8 * 3

 

 그리고 그 중 최대값을 고르면 된다.

 

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
29
30
31
32
33
package algorithm;
 
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
 
public class Problem2217 {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int N = in.nextInt();
        
        Integer[] arr = new Integer[N];
        
        for(int i=0;i<N;i++) {
            arr[i] = in.nextInt();
        }
        
        Arrays.sort(arr, Collections.reverseOrder());
        
        int answer = arr[0];
        
        for(int i=1;i<N;i++) {
            if(arr[i]*(i+1)>answer) {
                answer = arr[i]*(i+1);
            }
        }
 
        System.out.println(answer);
    }
}
 
cs