Skip to content

Commit

Permalink
Merge pull request #41 from AlgoLeadMe/12-mong3125
Browse files Browse the repository at this point in the history
12-mong3125
  • Loading branch information
mong3125 committed Apr 10, 2024
2 parents 465cfdb + 96a34b6 commit b75b28a
Showing 1 changed file with 66 additions and 0 deletions.
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;
}
}

0 comments on commit b75b28a

Please sign in to comment.