[BOJ 2343] ๊ธฐํ ๋ ์จ
1. ๋ฌธ์ : https://www.acmicpc.net/problem/2343
2. ํ์ด
๊ธฐ์กด ์์ ํฉ+ ์๋ก์ด ์์ > ๋น๋์ค ๊ธธ์ด -> +1
๊ธฐ์กด ์์ ํฉ์ 0์ผ๋ก ๋ง๋ค๊ณ ๋ค์ ๋ฐ๋ณต๋ฌธ
sum += arr[i] ํ๋ฉด 0์ผ๋ก ๋ง๋ ํ์๋ ๊ฐ ๋ํด์ฃผ๋ ๊ฒ ๊ฐ๋ฅ (์ธ๋ฑ์ค ๋ณ๊ฒฝ ๋ถํ์)
๋ฐ๋ณต๋ฌธ ๋ค ๋๊ณ sum์ด 0์ด ์๋๋ฉด ๋จ์ ๋น๋์ค๊ฐ ๋จ์์๋ค๋ ์๋ฆฌ๊ธฐ ๋๋ฌธ์ +1
upper bound, lower bound ์ ๋ฐ์ดํธ ํด์ฃผ๋ฉฐ ๋ฐ๋ณต
3. ์ฝ๋
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
int left = 0;
int right = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
right += arr[i];
left = Math.max(left, arr[i]);
}
while (left <= right) {
int mid = (left + right) / 2;
int sum = 0;
int k = 0;
for (int i = 0; i < n; i++) {
if (sum + arr[i] > mid) {
sum = 0;
k++;
}
sum += arr[i];
}
if (sum != 0) k++;
if (k > m) {
left = mid + 1;
} else {
right = mid - 1;
}
}
System.out.println(left);
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 2467] ์ฉ์ก (0) | 2023.09.11 |
---|---|
[BOJ 2110] ๊ณต์ ๊ธฐ ์ค์น (0) | 2023.09.06 |
[BOJ 19637] IF๋ฌธ ์ข ๋์ ์จ์ค (0) | 2023.08.31 |
[BOJ 2512] ์์ฐ (0) | 2023.08.27 |
[LeetCode] Add Two Numbers (0) | 2023.07.13 |
์ต๊ทผ๋๊ธ