ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/131127

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

๐Ÿ“  ํ’€์ด

์ด ๋ฌธ์ œ๋Š” Dictionary์™€ Set์„ ํ™œ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. ๋˜ํ•œ Sliding window ๊ฐœ๋…์„ ์ด์šฉํ•ด window์˜ ํฌ๊ธฐ๋ฅผ 10์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ์ƒˆ๋กœ ๋“ค์–ด์˜ค๋Š” ์›์†Œ์™€ ๋‚˜๊ฐ€๋Š” ์›์†Œ์— ์ฃผ๋ชฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค.

๋จผ์ € ์‚ฌ์•ผํ•˜๋Š” ๊ณผ์ผ์„ dictionary์— ์ €์žฅํ–ˆ๋‹ค. ๋˜ํ•œ ๋‚˜์ค‘์— ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ์›ํ•˜๋Š” ํ’ˆ๋ชฉ๋“ค์„ ์ „๋ถ€ ์ƒ€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ์œ„ํ•ด Set๋„ ๊ตฌํ–ˆ๋‹ค. Dictionary๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ Set์„ ์ด์šฉํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ž‘๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

wantList = dict()
for i in range(len(want)):
	wantList[want[i]] = number[i]
wantSet = set(wantList)

์šฐ์„  10์ผ๋™์•ˆ ํ’ˆ๋ชฉ๋“ค์„ ์‚ฐ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„๋•Œ ๋‚จ์€ ํ’ˆ๋ชฉ๊ณผ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” Dictionary์™€ Set๋„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

for i in range(10):
    if discount[i] in wantList.keys():
        wantList[discount[i]] -= 1 
    if wantList[discount[i]] == 0:
        wantSet.remove(discount[i]) // ์›ํ•˜๋Š” ํ’ˆ๋ชฉ์„ ๋‹ค์ƒ€์œผ๋ฉด Set์—์„œ ์ œ๊ฑฐํ•ด์ค€๋‹ค.
if len(wantSet) == 0: // ์›ํ•˜๋Š” ํ’ˆ๋ชฉ์„ ์ „๋ถ€ ์‚ฐ ๊ฒฝ์šฐ
    ans+=1

Sliding Window๋ฅผ ์ด์šฉํ•ด ๊ฐ๊ฐ ๋ฐ˜์˜ํ•ด์ค€๋‹ค.

for j in range(10, len(discount)):
    if discount[j] in wantList.keys(): // ์‚ฌ์•ผํ•˜๋Š” ํ’ˆ๋ชฉ์ธ ๊ฒฝ์šฐ
        wantList[discount[j]] -= 1
        if wantList[discount[j]] == 0: // ๋‹ค ์ƒ€์œผ๋ฉด Set์—์„œ ์ œ๊ฑฐ
            wantSet.remove(discount[j])
        
    if discount[j-10] in wantList.keys(): // 10์ผ ์ง€๋‚˜์„œ ํฌ๊ธฐํ•ด์•ผํ•˜๋Š” ํ’ˆ๋ชฉ
        wantList[discount[j-10]] += 1
        if wantList[discount[j-10]] == 1: // ๋‹ค์‹œ Set์— ์ถ”๊ฐ€
            wantSet.add(discount[j-10])
        
    if len(wantSet) == 0: // ๋‹ค์ƒ€์œผ๋ฉด
        ans+=1

๐Ÿ–ฅ ์ตœ์ข… ์ฝ”๋“œ

def solution(want, number, discount):
    ans = 0
    wantList = dict()
    
    for i in range(len(want)):
        wantList[want[i]] = number[i]
    wantSet = set(wantList)
        
    for i in range(10):
        if discount[i] in wantList.keys():
            wantList[discount[i]] -= 1
            if wantList[discount[i]] == 0:
                wantSet.remove(discount[i])
    if len(wantSet) == 0:
        ans+=1        
        
    for j in range(10, len(discount)):
        if discount[j] in wantList.keys():
            wantList[discount[j]] -= 1
            if wantList[discount[j]] == 0:
                wantSet.remove(discount[j])
        
        if discount[j-10] in wantList.keys():
            wantList[discount[j-10]] += 1
            if wantList[discount[j-10]] == 1:
                wantSet.add(discount[j-10])
        
        if len(wantSet) == 0:
            ans+=1 
                
    return ans
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ