๊ธฐ๋ก๋ฐฉ

BOJ_2003 : ์ˆ˜๋“ค์˜ ํ•ฉ 2 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2003 : ์ˆ˜๋“ค์˜ ํ•ฉ 2

Soom_1n 2022. 10. 26. 15:29

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2003๋ฒˆ: ์ˆ˜๋“ค์˜ ํ•ฉ 2

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 10,000), M(1 ≤ M ≤ 300,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” A[1], A[2], …, A[N]์ด ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ A[x]๋Š” 30,000์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ์ˆซ์ž์˜ ํ•ฉ์œผ๋กœ m์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • m์€ 3์–ต์ด๋ฏ€๋กœ intํ˜•์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค.
  • n๊ฐœ์˜ ์ˆ˜์˜ ํ•ฉ์ด m์ด ๋˜๋Š”์ง€ ๋งค๋ฒˆ ๋”ํ•ด์„œ ํ™•์ธํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค.
    • ๋”ฐ๋ผ์„œ ๋ฏธ๋ฆฌ ๋ˆ„์  ํ•ฉ ๋ฆฌ์ŠคํŠธ sum๋ฅผ ๋งŒ๋“ค์–ด ๋‘”๋‹ค.
    • ์ธ๋ฑ์Šค i๋ถ€ํ„ฐ ์ธ๋ฑ์Šค j ๊นŒ์ง€์˜ ํ•ฉ์€ sum[j] - sum[i] ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • sum[0]์€ 0์ด๋‹ค.
    • sum[j] - sum[i] > m์ด๋ฉด, ๊ณ„์† ์ปค์ง€๋Š” ๋ฐฉํ–ฅ์ธ ๋‹ค์Œ j๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฑธ ๋ฉˆ์ถ”๊ณ  ๋‹ค์Œ i๋ฅผ ํ™•์ธํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

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());

        final int n = Integer.parseInt(st.nextToken());
        final int m = Integer.parseInt(st.nextToken());
        int sum[] = new int[n+1];
        sum[0] = 0;

        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= n; i++){
            sum[i] = sum[i-1] + Integer.parseInt(st.nextToken());
        }

        int answer = 0;
        for (int i = 0; i <= n; i++){
            for (int j = i + 1; j <= n; j++){
                if (sum[j] - sum[i] == m) {
                    answer++;
                    break;
                }
            }
        }
        System.out.println(answer);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ์ˆ˜๋ฅผ sum[1]๋ถ€ํ„ฐ sum[n]์— ์ €์žฅํ•œ๋‹ค.
    • sum[0] ์€ 0์ด๋‹ค.
    • i>0์ผ๋•Œ, sum[i] = sum[i-1] + ์ž…๋ ฅ์ˆ˜๋กœ ๋ˆ„์ ํ•ด๊ฐ€๋ฉฐ ์ €์žฅํ•œ๋‹ค.
  • i๋ถ€ํ„ฐ j์˜ ํ•ฉ์€ sum[j] - sum[i]๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ฐ’์ด m์ด๋ฉด answer++
    • j๋ฅผ ๋” ํ‚ค์›Œ๋ดค์ž m์„ ์ดˆ๊ณผํ•˜๋ฏ€๋กœ breakํ•ด์ค€๋‹ค.

 


๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ถ€๋ถ„ ํ•ฉ ๋ฌธ์ œ๋กœ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ํƒœ๊ทธ๋Š” ํˆฌ ํฌ์ธํ„ฐ๋กœ ๋‹ฌ๋ ค์žˆ์—ˆ๋‹ค.
  • ๋‹ค์‹œ ๊ฐœ๋…์„ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋ถ€๋ถ„ ํ•ฉ ๊ฐœ๋…์•ˆ์— ํˆฌ ํฌ์ธํ„ฐ๊ฐ€ ๋…น์•„์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ฝ”๋“œ์—์„œ sum[i] - sum[j] == m ์ธ ๊ฒฝ์šฐ์—๋งŒ ์กฐ๊ธฐ ์ข…๋ฃŒ๋˜๋Š”๋ฐ, sum[i] - sum[j] > m ๊ฒฝ์šฐ์—๋„ ์กฐ๊ธฐ ์ข…๋ฃŒ๋ฅผ ํ•ด์ค˜์•ผ ๋” ๋น ๋ฅธ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์ˆ˜์ •ํ›„ ์ฑ„์ ํ•ด ๋ดค์ง€๋งŒ ์‹œ๊ฐ„์€ ๋™์ผํ–ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์ฐจ์ด๊ฐ€ ์•ˆ๋‚˜๋Š” ์ผ€์ด์Šค ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1059 : ์ข‹์€ ๊ตฌ๊ฐ„  (0) 2022.10.30
BOJ_1051 : ์ˆซ์ž ์ •์‚ฌ๊ฐํ˜•  (0) 2022.10.27
BOJ_11004 : K๋ฒˆ์žฌ ์ˆ˜  (0) 2022.10.25
BOJ_6996 : ์• ๋„ˆ๊ทธ๋žจ  (0) 2022.10.24
BOJ_10989 : ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 3  (0) 2022.10.23