-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboat.java
More file actions
38 lines (33 loc) · 908 Bytes
/
boat.java
File metadata and controls
38 lines (33 loc) · 908 Bytes
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
34
35
36
37
38
package org.programmers;
import java.util.Arrays;
public class boat {
public static void main(String[] args) {
int[] p = {70,80,50};
solution(p, 100);
}
public static int solution(int[] people, int limit) {
int answer = 0;
Arrays.sort(people);
int head = 0;
for(int i=people.length-1; i >= 0; i--) {
// 마지막 남은 한 사람
if (i==head) {
answer++;
break;
}
// 제일 몸무게 큰사람 + 작은 사람 이 limit보다 작으면 같이 태워보낸다
if (people[i] + people[head] <= limit) {
head++;
answer++;
// 마지막 남은 두사람이었다면 for문 나간다
if (i==head) {
break;
}
} else {
// 제일 몸무게 큰사람만 태워 보낸다
answer++;
}
}
return answer;
}
}