forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYJ_42885.java
More file actions
33 lines (29 loc) · 879 Bytes
/
YJ_42885.java
File metadata and controls
33 lines (29 loc) · 879 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
import java.util.*;
/**
* 알고리즘: 투포인터 O(NlogN)
* 시간복잡도: 1 <= N <= 50,000
* 최악의 경우 모든 지점을 다 거치면 O(N)
* 정렬 필요 O(NlogN)
* 아이디어:
* 맨 앞 가벼운 사람 + 맨 뒤 무거운 사람이 동승 가능한지 체크
*/
public class YJ_42885 {
public static void main(String[] args) {
int[] people = {70, 80, 50};
int limit = 100;
System.out.println(getBoatCount(people, limit));
}
static int getBoatCount (int[] people, int limit) {
int boat = 0;
Arrays.sort(people);
int start = 0;
for(int end=people.length-1; start<=end; end--){
//기준값보다 작거나 같을 경우
if(people[start] + people[end] <= limit){
start++;
}
boat++;
}
return boat;
}
}