Post

[백준] 2003번 - 수들의 합 2 [Java][C++]

[백준] 2003번 - 수들의 합 2 [Java][C++]

문제 링크


1. 문제 풀이

구간합이 $M$ 이 되는 경우의 수를 세는 문제로 구간합을 효율적으로 구할 수 있는 누적합을 활용하거나 투 포인터를 활용하면 해결할 수 있다.


2. 코드

1. 누적합 [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
30
31
32
import java.io.*;
import java.util.*;

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];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] pSum = new int[1 + N];
        for (int i = 1; i <= N; i++) {
            pSum[i] = pSum[i - 1] + arr[i - 1];
        }

        int cnt = 0;
        for (int i = 1; i <= N; i++) {
            for (int j = i; j <= N; j++) {
                if (pSum[j] - pSum[i - 1] == M) cnt++;
            }
        }

        System.out.println(cnt);
    }
}

2. 투 포인터 [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
30
31
32
33
34
35
36
import java.io.*;
import java.util.*;

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];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int left = 0;
        int right = 0;
        int sum = 0;
        int cnt = 0;
        while (true) {
            if (sum < M) {
                sum += arr[right++];
            } else {
                if (sum == M) cnt++;

                sum -= arr[left++];
            }

            if (right == N && sum < M) break;
        }

        System.out.println(cnt);
    }
}

3. 누적합 [C++]

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
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<int> v(n);
    for (int& x : v) cin >> x;

    vector<int> psum(1 + n);
    for (int i = 1; i <= n; i++) {
        psum[i] = psum[i - 1] + v[i - 1];
    }

    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            if (psum[j] - psum[i - 1] == m) cnt++;
        }
    }

    cout << cnt;
}

4. 투 포인터 [C++]

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
30
31
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<int> v(n);
    for (int& x : v) cin >> x;

    int l = 0;
    int r = 0;
    int sum = 0;
    int cnt = 0;
    while (true) {
        if (sum < m) {
            sum += v[r++];
        } else {
            if (sum == m) cnt++;

            sum -= v[l++];
        }

        if (r == n && sum < m) break;
    }

    cout << cnt;
}

3. 풀이 정보

1. 누적합 [Java]

언어시간메모리코드 길이
Java 11212 ms15204 KB916 B

2. 투 포인터 [Java]

언어시간메모리코드 길이
Java 11124 ms15044 KB939 B

3. 누적합 [C++]

언어시간메모리코드 길이
C++ 1744 ms2180 KB495 B

4. 투 포인터 [C++]

언어시간메모리코드 길이
C++ 170 ms2180 KB490 B

This post is licensed under CC BY 4.0 by the author.