[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ4] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 10816 - ์ˆซ์ž ์นด๋“œ 2

keyneneยท2022๋…„ 10์›” 15์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
5/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ4] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 10816 - ์ˆซ์ž์นด๋“œ2

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

์ด๋ถ„ํƒ์ƒ‰(binary_search)ํ•˜์—ฌ ์›ํ•˜๋Š” ์นด๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„๋‚ด๋Š” bs()ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , N,M,๊ฐ ์นด๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ dictionary์— ์ €์žฅํ•˜์ž
(์นด๋“œ ๊ฐœ์ˆ˜๋Š” dictionary์˜ .get()์„ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ ธ์˜ด)
๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ bs()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด M์˜ ๊ฐ ์นด๋“œ๊ฐ€ N์— ๋ช‡๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ ํƒ์ƒ‰ํ•˜์—ฌ ์ถœ๋ ฅํ•ด๋ณด์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. bs(arr, target, start, end)ํ•จ์ˆ˜ ๊ตฌํ˜„
  2. N, M ๋“ฑ ๊ฐ ์นด๋“œ ์ž…๋ ฅ๋ฐ›๊ธฐ
  3. dictionary ์ •์˜ ๋ฐ ๊ฐ’ ๋„ฃ๊ธฐ
  4. M์นด๋“œ์ˆ˜๋งŒํผ bs()ํ˜ธ์ถœํ•˜์—ฌ dictionary์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์นด๋“œ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys

def bs(arr, target, start, end):
    if start > end:
        return 0
    mid = (start+end)//2
    if arr[mid] == target :
    	#target ์ผ์น˜ํ• ๊ฒฝ์šฐ dictionary์˜ ๊ฐ’ return
        return cnt.get(target)
    elif arr[mid] > target :
    	#num[mid]๊ฐ’์ด M์˜ ํ•ด๋‹น ์นด๋“œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด end = mid-1
        return bs(arr, target, start, mid-1)
    else:
    	#num[mid]๊ฐ’์ด M์˜ ํ•ด๋‹น ์นด๋“œ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด start = mid+1
        return bs(arr, target, mid+1, end)

n = int(sys.stdin.readline().rstrip())
#N์˜ ์นด๋“œ๋Š” ์ •๋ ฌํ•˜์—ฌ ์ €์žฅ (โ€ป์ด๋ถ„ํƒ์ƒ‰ ๊ธฐ๋ณธ์กฐ๊ฑด! arr์ •๋ ฌํ•˜๊ธฐ)
num = sorted(list(map(int, sys.stdin.readline().split())))
m = int(sys.stdin.readline().rstrip())
comp = list(map(int, sys.stdin.readline().split()))

#๋นˆ dictionary
cnt = {} 

#num๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ "์นด๋“œ:๊ฐœ์ˆ˜"ํ˜•ํƒœ๋กœ ์ €์žฅ
for i in num :
	#dictionary์— num์˜ i๊ฐ’์ด ์žˆ๋‹ค๋ฉด +1
    if i in cnt :
        cnt[i] += 1
    #dictionary์— num์˜ i๊ฐ’์ด ์—†๋‹ค๋ฉด, cnt[i]๋งŒ๋“ค๊ณ  1์ €์žฅ 
    else :
        cnt[i] = 1

for i in comp :
    print(bs(num, i, 0, len(num)-1), end=' ')

โœ๏ธ1. ์ด๋ถ„ํƒ์ƒ‰(binary_search)ํ•จ์ˆ˜ ๊ตฌํ˜„

def bs(arr, target, start, end):
    if start > end:
        return 0
    mid = (start+end)//2
    if arr[mid] == target :
        return cnt.get(target) #dictionary ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
    elif arr[mid] > target :
        return bs(arr, target, start, mid-1)
    else:
        return bs(arr, target, mid+1, end)
โ€ป์ด๋ถ„ํƒ์ƒ‰(binary_search)
  1. ๋ฐฐ์—ด(arr)์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค
  2. ๋น„๊ตํ•  ๊ฐ’(target)์ด arr์˜ ์ค‘๊ฐ„(arr[mid])์— ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด,
     end = mid-1ํ•˜์—ฌ ์ด๋ถ„ํƒ์ƒ‰์„ ์žฌ์‹คํ–‰ํ•œ๋‹ค
     (ex/ 0~9๋ฐฐ์—ด์— target์ด arr[4]๋ณด๋‹ค ์ž‘์œผ๋ฉด, 0~3์‚ฌ์ด๋ฅผ ์žฌ์‹คํ–‰)
  2. ๋น„๊ตํ•  ๊ฐ’(target)์ด arr์˜ ์ค‘๊ฐ„(arr[mid])์— ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด,
     start = mid+1ํ•˜์—ฌ ์ด๋ถ„ํƒ์ƒ‰์„ ์žฌ์‹คํ–‰ํ•œ๋‹ค
     (ex/ 0~9๋ฐฐ์—ด์— target์ด arr[4]๋ณด๋‹ค ํฌ๋ฉด, 5~9์‚ฌ์ด๋ฅผ ์žฌ์‹คํ–‰)
  3. ๋งˆ์นจ๋‚ด ๋น„๊ตํ•  ๊ฐ’(target)์ด arr[mid]์™€ ๊ฐ™์œผ๋ฉด ํ•ด๋‹น ๊ฐ’ return
  
โ€ปํƒ์ƒ‰ํ•จ์ˆ˜๋Š” ์œ„ ์˜ˆ์ œ์ฒ˜๋Ÿผ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด๋„ ๋˜์ง€๋งŒ,
  while start >= end์™€ if/else๋กœ ๋ฐ˜๋ณต์‹คํ–‰๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

โœ๏ธ2. N, M ๋“ฑ ๊ฐ’ ์ž…๋ ฅ๋ฐ›๊ธฐ

import sys

n = int(sys.stdin.readline().rstrip())
num = sorted(list(map(int, sys.stdin.readline().split())))
m = int(sys.stdin.readline().rstrip())
comp = list(map(int, sys.stdin.readline().split()))

โœ๏ธ3. dictionary์— "์นด๋“œ:๊ฐœ์ˆ˜"ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ธฐ

#๋นˆ dinctionary ์„ ์–ธ
#์—ฌ๊ธฐ์— {์นด๋“œ:๊ฐœ์ˆ˜, ...} โ†’ {-10:2 ...}ํ˜•ํƒœ๋กœ ์ €์žฅ๋ ๊บผ์ž„
cnt = {}

for i in num :

	#cnt์— num์˜ i๊ฐ€ ์žˆ์œผ๋ฉด +1
    #ex) cnt์•ˆ์— -10:1 ์ด ์žˆ์œผ๋ฉด -10:2์œผ๋กœ +1ํ•ด์คŒ
    if i in cnt :
        cnt[i] += 1
    
    #cnt์— num์˜ i๊ฐ€ ์—†์œผ๋ฉด ๋“ฑ๋ก
    #ex) -10์—†์œผ๋ฉด -10:1 ๋“ฑ๋ก
    else :
        cnt[i] = 1
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›€
โ€ปcnt.get(-10)ํ˜•์‹์œผ๋กœ ๊ฐ’ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ

โœ๏ธ4. bs()ํ˜ธ์ถœํ•˜์—ฌ dictionary์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ์นด๋“œ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

for i in comp :
    print(bs(num, i, 0, len(num)-1), end=' ')

๐Ÿ“š๊ธฐ์กด ๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

1. ์ž‰? ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ?

import sys

n = int(sys.stdin.readline().strip())
num = list(map(int, sys.stdin.readline().split()))

m = int(sys.stdin.readline().strip())
comp = list(map(int, sys.stdin.readline().split()))

res = ''
for i in comp :
    cnt = 0
    for j in num :
        if i == j :
            cnt += 1
    res += str(cnt)+' '
print(res)
๐Ÿ™…๐Ÿปโ€โ™€๏ธ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•œ ๊ฒƒ์ด ๋ฌธ์ œ!
   ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ O(N)๋ณด๋‹ค ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ์š”ํ–ˆ์Œ

์•„! ์ด๋ถ„ํƒ์ƒ‰(binary_search)์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๊ตฐ
์ด์ œ์•ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋‹ค์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋Š” ๊ฒƒ ๊ฐ™๋„ค
์ด๋ถ„ํƒ์ƒ‰ ํ•จ์ˆ˜ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๋ฉด ๋๋‚˜๊ฒ ๋„ค?๐Ÿคท๐Ÿปโ€โ™€๏ธ

2. ํ•˜.. ๋Ÿฐํƒ€์ž„์—๋Ÿฌ

<import sys

n = int(sys.stdin.readline().rstrip())
num = list(map(int, sys.stdin.readline().split()))
s_num = sorted(num)

m = int(sys.stdin.readline().rstrip())
comp = list(map(int, sys.stdin.readline().split()))

def binary(arr, target, start, end) :
    cnt = 0

    mid = (start+end)//2

    if start > end:
        return 0
    elif arr[mid] == target :
        cnt = num.count(arr[mid])
        return str(cnt)
    elif arr[mid] > target :
        return (binary(arr, target, start, mid-1))
    else:
        return (binary(arr, target, start+1, end))


for i in comp :
    print(binary(s_num, i, 0, len(num)-1), end=' ')
๐Ÿ™…๐Ÿปโ€๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ (IndexError)
   ใ… ใ…  ์ œ์ผ ์ดํ•ด์•ˆ๊ฐ€๊ณ  ์—๋Ÿฌ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋Š” ์—๋Ÿฌ๋‹ค.
   ํ•ด๋‹น ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚œ ์ด์œ ๋Š” cnt=num.count(arr[mid])์ธ ๊ฒƒ์œผ๋กœ ์ถ”์ •!
   ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ๊นŒ .count()๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน๋Š” ๊ฒƒ ๊ฐ™์Œ

  1. ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ๋Š” ํƒ์ƒ‰ ๊ธฐ์ค€์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด์ž
    (๋ฌด์กฐ๊ฑด ๋ธŒ๋ฃจํŠธํฌ์Šค๊ฐ€ ๋‹ต์ด ์•„๋‹ˆ๋‹ค)
  2. ์ด๋ถ„ํƒ์ƒ‰(binary_search)๋Š” ๋‹จ๊ณจ๋ฌธ์ œ๋‹ค. ์ž˜ ๊ธฐ์–ตํ•˜์ž
    (dictionary๋Š” ํ•œ ์„ธํŠธ๋กœ ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•จ)
  3. ์ด๋ถ„ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋˜๋ฐ....
    (์ถ”ํ›„ ์•Œ์•„๋ณด๊ณ  ํฌ์ŠคํŒ…ํ•˜๊ธฐ)
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€