1. ๋ฌธ์ : https://www.acmicpc.net/problem/1806
2. ํ์ด
ํฌํฌ์ธํฐ ์ด์ฉํด์ ์ต์๊ฐ ์ฐพ๋ ๋ฌธ์
total์ด s๋ฅผ ๋์ด๊ฐ๋ ์๊ฐ์ ์ก์์ min๊ฐ์ ๊ณ์ ์ ๋ฐ์ดํธ ํด์ค
3. ์ฝ๋
import java.util.Scanner;
public class ๋ถ๋ถํฉ {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int s = scan.nextInt();
int[] nums = new int[n + 1];
for (int i = 0; i < n; i++) {
nums[i] = scan.nextInt();
}
int min = Integer.MAX_VALUE;
int start = 0;
int end = 0;
int total = 0;
while (start <= n && end <= n) {
if (total >= s && min > end - start) min = end - start;
if (total < s) total += nums[end++];
else total -= nums[start++];
}
if (min == Integer.MAX_VALUE) System.out.println("0");
else System.out.println(min);
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ (์๋ฐ java) (0) | 2023.05.15 |
[LeetCode] 3Sum (0) | 2023.04.12 |
[LeetCode] Container With Most Water (0) | 2023.03.28 |
[BOJ 14719] ๋น๋ฌผ (0) | 2022.06.26 |
์ต๊ทผ๋๊ธ