forked from wotjd4305/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproBinary1.java
More file actions
74 lines (58 loc) · 1.63 KB
/
proBinary1.java
File metadata and controls
74 lines (58 loc) · 1.63 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Arrays;
public class proBinary1 {
static int answer = 0;
public static void main(String[] args) {
int budgets[] = {122,122,300,600};
int M = 485;
System.out.println(solution(budgets, M));
}
public static int solution(int[] budgets, int M) {
//오름차순 정렬, 110 120 140 150
Arrays.sort(budgets);
int total =0;
int avg;
int low = 0;
int high = budgets[budgets.length-1];
int temp = 0;
boolean flag = false;
//처음에 초기화
avg = (low+high)/2;
while(true)
{
total = 0;
temp = avg;
//안바뀌면 이게 답, flag없으면 안돌고와서 0 == 0 해서 바로끝남
if(temp == (low+high)/2 && flag)
{
answer = avg;
break;
}
avg = (low+high) / 2;
//넘으면 넘긴게 빼고고
for(int i=0; i<budgets.length; i++)
{
if(avg <= budgets[i])
total += avg;
else
total += budgets[i];
}
//예산 초과면, low를 높여서 더 제한
if(total > M)
{
high = (high+low)/2 -1;
}
else if(total < M)
{
low = (high+low)/2 +1; // 0->1
}
else//예산에 딱맞다
{
answer = avg;
break;
}
//한번은 돌았다.
flag = true;
}
return answer;
}
}