Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12-mong3125 #41

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package 이진탐색;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class BOJ2343_κΈ°νƒ€λ ˆμŠ¨ {

static int N;
static int M;
static int[] lectures;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

lectures = new int[N];
st = new StringTokenizer(br.readLine());

int sum = 0;
int max = 0;
for(int i = 0; i < N; i++) {
lectures[i] = Integer.parseInt(st.nextToken());
sum += lectures[i];
max = Math.max(max, lectures[i]);
}

// λΈ”λ£¨λ ˆμ΄μ˜ ν¬κΈ°λŠ” maxλΆ€ν„° sum 사이 일것이닀.
int result = minimumBlueray(max, sum);

System.out.println(result);
}

public static int minimumBlueray(int start, int end) {
if (start < end) return -1;

int mid = (start + end) / 2;

if (can(mid)) {
int more = minimumBlueray(start, mid - 1);
return more != -1 ? more : mid;
}
else return minimumBlueray(mid + 1, end);
}

public static boolean can(int blueray) {
int count = M;
int i = 0;
int stack = blueray;
while (count > 0) {
while (stack - lectures[i] > 0) {
if (i == N) return true;
stack -= lectures[i];
i++;
}

stack = blueray;
count--;
}

return false;
Comment on lines +49 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μœ„μ˜ can() ν•¨μˆ˜λ‘œ mid μ‚¬μ΄μ¦ˆλ‘œ λ…Ήν™”κ°€ κ°€λŠ₯ν•œμ§€ νŒλ…ν•˜λŠ”κ±Έλ‘œ λ³΄μž…λ‹ˆλ‹€.
mid 값을 담은 stackμ—μ„œ κ°•μ˜μ‹œκ°„μ„ ν•˜λ‚˜μ”© λΉΌκ°€λ©΄μ„œ λͺ¨λ“  κ°•μ˜κ°€ M개의 λΈ”λ£¨λ ˆμ΄μ— λ‹΄κΈΈ 수 μžˆλŠ”μ§€ νŒλ…ν•˜λŠ”κ²ƒμœΌλ‘œ μ΄ν•΄ν•˜λ©΄ λ κΉŒμš”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ„€ λ§žμŠ΅λ‹ˆλ‹€!

}
}