forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.java
29 lines (25 loc) ยท 843 Bytes
/
3.java
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
import java.util.*;
class Main {
public static int n = 5; // ๋ฐ์ดํฐ์ ๊ฐ์ N
public static int m = 5; // ์ฐพ๊ณ ์ ํ๋ ๋ถ๋ถํฉ M
public static int[] arr = {1, 2, 3, 2, 5}; // ์ ์ฒด ์์ด
public static void main(String[] args) {
int cnt = 0;
int intervalSum = 0;
int end = 0;
// start๋ฅผ ์ฐจ๋ก๋๋ก ์ฆ๊ฐ์ํค๋ฉฐ ๋ฐ๋ณต
for (int start = 0; start < n; start++) {
// end๋ฅผ ๊ฐ๋ฅํ ๋งํผ ์ด๋์ํค๊ธฐ
while (intervalSum < m && end < n) {
intervalSum += arr[end];
end += 1;
}
// ๋ถ๋ถํฉ์ด m์ผ ๋ ์นด์ดํธ ์ฆ๊ฐ
if (intervalSum == m) {
cnt += 1;
}
intervalSum -= arr[start];
}
System.out.println(cnt);
}
}