less than 1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 4


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1940


image


๐Ÿ“Œ ํ’€์ด

๋ถ„๋ช…ํžˆ Two Pointer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์ด์ง€๋งŒ, ๊ทธ๋ ‡๊ฒŒ ์•ˆํ•˜๊ณ  O(n x log(n)) ์ •๋„์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„ for๋ฌธ์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜์˜€๋‹ค.
๊ฐ„๋‹จํ•˜๊ฒŒ 0-n ๊นŒ์ง€์˜ i๊ฐ’๊ณผ i-n ๊นŒ์ง€์˜ j๊ฐ’์„ ํ†ตํ•œ ์ด์ค‘ for๋ฌธ์œผ๋กœ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

์ž๋ฐ”๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์„œ ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๋„๋„ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿ“Œ Code

package TwoPointer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class N1940 {
    static int[] material;
    static int count;
    static int target;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        count = Integer.parseInt(br.readLine());
        target = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        material = new int[count];
        for (int i = 0; i < count; i++) {
            material[i] = Integer.parseInt(st.nextToken());
        }
        MakeArmor();
    }

    public static void MakeArmor() {
        int result = 0;
        for (int i = 0; i < count; i++) {
            for (int j = i + 1; j < count; j++) {
                if (material[i] + material[j] == target) {
                    result += 1;
                    break;
                }
            }
        }
        System.out.println(result);
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜