๐Ÿ‘๐Ÿป ์ข‹๋‹ค

๋ฐฑ์ค€ Gold4

์ข‹๋‹ค

๋ฌธ์ œ


N๊ฐœ์˜ ์ˆ˜ ์ค‘์—์„œ ์–ด๋–ค ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ์ˆ˜ ๋‘ ๊ฐœ์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ ์ˆ˜๋ฅผ โ€œ์ข‹๋‹ค(GOOD)โ€๊ณ  ํ•œ๋‹ค.

N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ทธ ์ค‘์—์„œ ์ข‹์€ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋ผ.

์ˆ˜์˜ ์œ„์น˜๊ฐ€ ๋‹ค๋ฅด๋ฉด ๊ฐ’์ด ๊ฐ™์•„๋„ ๋‹ค๋ฅธ ์ˆ˜์ด๋‹ค.

์ž…๋ ฅ


์ฒซ์งธ ์ค„์—๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 โ‰ค N โ‰ค 2,000), ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” i๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Ai๊ฐ€ N๊ฐœ ์ฃผ์–ด์ง„๋‹ค. (|Ai| โ‰ค 1,000,000,000, Ai๋Š” ์ •์ˆ˜)

์ถœ๋ ฅ


์ข‹์€ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

10
1 2 3 4 5 6 7 8 9 10

์˜ˆ์ œ ์ถœ๋ ฅ 1

8

ํžŒํŠธ


3,4,5,6,7,8,9,10์€ ์ข‹๋‹ค.

๐Ÿ“– Two Pointer

ํˆฌํฌ์ธํ„ฐ๋Š” [ํŠน์ •ํ•œ ํ•ฉ์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„ ์—ฐ์† ์ˆ˜์—ด]์„ ๊ตฌํ• ๋•Œ ์“ด๋‹ค.

๊ธ€๋กœ ํ‘œํ˜„ํ•˜๊ธฐ์—๋Š” ์ดํ•ด๊ฐ€ ์•ˆ ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•˜์—ฌ ์„ค๋ช…์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-36

๋ฐฐ์—ด์ด 1,2,3,2,5๋กœ ์ฃผ์–ด์ง€๊ณ  ๋ฐฐ์—ด์˜ ์—ฐ์†ํ•ฉ์ด โ€œ5โ€๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ฐพ๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

์‹œ์ž‘ ํฌ์ธํ„ฐ์™€ ๋ ํฌ์ธํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๊ณ  ๋ ํฌ์ธํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉฐ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•œ๋‹ค.

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-37

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋ˆ„์ ํ•ฉ์ด ์ฐพ๊ณ ์ž ํ•˜๋Š” ์ˆ˜๋ณด๋‹ค ์ปค์ง€๋Š” ์ˆœ๊ฐ„์ด ์˜จ๋‹ค๋ฉด ์‹œ์ž‘ ํฌ์ธํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ธด๋‹ค.

์™œ๋ƒํ•˜๋ฉด ํฌ์ธํ„ฐ๋ฅผ ์กฐ์ •ํ•˜์—ฌ ๋ˆ„์ ํ•ฉ์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ๋ˆ„์ ํ•ฉ โฌ‡๏ธ ๐Ÿ‘‰๐Ÿป ์‹œ์ž‘ ํฌ์ธํ„ฐ ์˜ค๋ฅธ์ชฝ
  • ๋ˆ„์ ํ•ฉ โฌ†๏ธ ๐Ÿ‘‰๐Ÿป ๋ ํฌ์ธํ„ฐ ์˜ค๋ฅธ์ชฝ

๋งŒ์•ฝ, ๋ˆ„์ ํ•ฉ์ด ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๊ณ ์ž ํ•˜๋Š” ์ˆ˜์™€ ๊ฐ™๋‹ค๋ฉด ๊ฐœ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-38

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-39

์ด๋ฅผ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ์ตœ์ข…์ ์œผ๋กœ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

์™œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

์ด ๋ฌธ์ œ์—์„œ๋Š” [์–ด๋–ค ์ˆ˜]๊ฐ€ [๋‹ค๋ฅธ ์ˆ˜ ๋‘ ๊ฐœ์˜ ํ•ฉ]์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ฌป๊ณ  ์žˆ๋‹ค.

[์–ด๋–ค ์ˆ˜] ๐Ÿ‘‰๐Ÿป ํŠน์ •ํ•œ ํ•ฉ [๋‹ค๋ฅธ ์ˆ˜ ๋‘ ๊ฐœ์˜ ํ•ฉ] ๐Ÿ‘‰๐Ÿป ๋ถ€๋ถ„ ์—ฐ์† ์ˆ˜์—ด

์œ„์™€ ๊ฐ™์ด ํ•ด๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํˆฌํฌ์ธํ„ฐ๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๋ฌผ๋ก  ๋‚˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜๋ฅผ ๋ณด๊ณ  ๊นจ๋‹ฌ์•˜๋‹ค. ํ•˜ํ•˜ํ•˜ ๐Ÿ˜…

๐Ÿ“ ์„ค๊ณ„

TRY 1

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-41

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-42

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-43

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-44

์ฒซ ๋ฒˆ์งธ ์„ค๊ณ„์˜ ์‹คํŒจ ์ด์œ ๋Š” ํฌ์ธํ„ฐ ์œ„์น˜๋ฅผ ๋™์ผํ•œ ๊ฒฝ์šฐ๋„ ํฌํ•จ์‹œ์ผฐ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํˆฌํฌ์ธํ„ฐ์— ๋Œ€ํ•˜์—ฌ ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋“ค์ด ์กฐ๊ธˆ ์žˆ์—ˆ์ง€๋งŒ ๊ทธ ๋™์•ˆ ํ—›๊ณต๋ถ€๋ฅผ ํ•œ ๊ฒƒ์ธ์ง€ ๊ธฐ์–ต์ด ์ œ๋Œ€๋กœ ์•ˆ ๋‚ฌ๋‹ค.

๊ทธ๋ž˜์„œ ์ถ”๊ฐ€์ ์ธ ๊ณต๋ถ€๋ฅผ ํ•œ ๋‚ด์šฉ์ด ์œ„์˜ ๋‚ด์šฉ์ธ๋ฐ ์˜ˆ์‹œ์— ๋Œ€ํ•œ ๊ธฐ์–ต์ด ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ์–ด์„œ์ธ์ง€ ๋ฌด์˜์‹์ ์œผ๋กœ ๋ฌธ์ œ ์กฐ๊ฑด์„ ๊ฐ„๊ณผํ•œ ์ฑ„ ํฌ์ธํ„ฐ๋ฅผ ๊ฒน์ณ๋†“์•˜๋‹ค.

์ด ๋ฌธ์ œ์—์„œ ๋ฌป๋Š” ๊ฒƒ์€ 2๊ฐœ์˜ ์ˆ˜๋ฅผ ํ•ฉํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ํฌ์ธํ„ฐ๊ฐ€ ๊ฒน์ณ์ง€๊ฒŒ ๋˜๋ฉด 1๊ฐœ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋ฐ˜๋ก€๊ฐ€ ์ƒ๊ธด๋‹ค.

TRY 2

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-45

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-46

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-47

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-48

[์‹œ์ž‘ ํฌ์ธํ„ฐ < ๋ ํฌ์ธํ„ฐ] ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•˜์—ฌ ๋ ํฌ์ธํ„ฐ๋ฅผ ๋ฐฐ์—ด ๋๋ถ€ํ„ฐ ์„ค์ •ํ•ด๋†“๊ณ  ์ด๋™์‹œ์ผฐ๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

TRY 1

import sys
input = sys.stdin.readline

N = int(input())
arr = list(map(int,input().split()))

arr.sort()
good_count = 0

for i in range(N):
    check = arr[:i] + arr[i+1:]
    summary = 0
    m = arr[i]
    end = 0

    for start in range(N-1):
        if check[start] > m:
            break
        while summary < m and end < N-1:
            if start == end:
                summary = check[start]
            else:
                summary = check[start]+check[end]
            end +=1
        if summary == m:
            good_count += 1
            break

print(good_count)

TRY 2

import sys
input = sys.stdin.readline

N = int(input())
arr = list(map(int,input().split()))

arr.sort()
good_count = 0

for i in range(N):
    check = arr[:i] + arr[i+1:]
    start,end = 0,N-2
    sum = 0
    target = arr[i]

    while start < end:
        sum = check[start]+check[end]

        if sum == target:
            good_count += 1
            break
        
        if sum > target:
            end -= 1
        
        if sum < target:
            start += 1

print(good_count)

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6