๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋กค์ผ€์ดํฌ ์ž๋ฅด๊ธฐ

๐ŸŽฎinspirer9 2023. 2. 18. 23:12
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์ฒ ์ˆ˜๋Š” ๋กค์ผ€์ดํฌ๋ฅผ ๋‘ ์กฐ๊ฐ์œผ๋กœ ์ž˜๋ผ์„œ ๋™์ƒ๊ณผ ํ•œ ์กฐ๊ฐ์”ฉ ๋‚˜๋ˆ  ๋จน์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋กค์ผ€์ดํฌ์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ† ํ•‘๋“ค์ด ์ผ๋ ฌ๋กœ ์˜ฌ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒ ์ˆ˜์™€ ๋™์ƒ์€ ๋กค์ผ€์ดํฌ๋ฅผ ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋ˆ ๋จน์œผ๋ ค ํ•˜๋Š”๋ฐ, ๊ทธ๋“ค์€ ๋กค์ผ€์ดํฌ์˜ ํฌ๊ธฐ๋ณด๋‹ค ๋กค์ผ€์ดํฌ ์œ„์— ์˜ฌ๋ ค์ง„ ํ† ํ•‘๋“ค์˜ ์ข…๋ฅ˜์— ๋” ๊ด€์‹ฌ์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž˜๋ฆฐ ์กฐ๊ฐ๋“ค์˜ ํฌ๊ธฐ์™€ ์˜ฌ๋ ค์ง„ ํ† ํ•‘์˜ ๊ฐœ์ˆ˜์— ์ƒ๊ด€์—†์ด ๊ฐ ์กฐ๊ฐ์— ๋™์ผํ•œ ๊ฐ€์ง“์ˆ˜์˜ ํ† ํ•‘์ด ์˜ฌ๋ผ๊ฐ€๋ฉด ๊ณตํ‰ํ•˜๊ฒŒ ๋กค์ผ€์ดํฌ๊ฐ€ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋กค์ผ€์ดํฌ์— 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํ† ํ•‘์ด ์˜ฌ๋ ค์ ธ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ํ† ํ•‘๋“ค์„ 1, 2, 3, 4์™€ ๊ฐ™์ด ๋ฒˆํ˜ธ๋กœ ํ‘œ์‹œํ–ˆ์„ ๋•Œ, ์ผ€์ดํฌ ์œ„์— ํ† ํ•‘๋“ค์ด [1, 2, 1, 3, 1, 4, 1, 2] ์ˆœ์„œ๋กœ ์˜ฌ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„ธ ๋ฒˆ์งธ ํ† ํ•‘(1)๊ณผ ๋„ค ๋ฒˆ์งธ ํ† ํ•‘(3) ์‚ฌ์ด๋ฅผ ์ž๋ฅด๋ฉด ๋กค์ผ€์ดํฌ์˜ ํ† ํ•‘์€ [1, 2, 1], [3, 1, 4, 1, 2]๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฒ ์ˆ˜๊ฐ€ [1, 2, 1]์ด ๋†“์ธ ์กฐ๊ฐ์„, ๋™์ƒ์ด [3, 1, 4, 1, 2]๊ฐ€ ๋†“์ธ ์กฐ๊ฐ์„ ๋จน๊ฒŒ ๋˜๋ฉด ์ฒ ์ˆ˜๋Š” ๋‘ ๊ฐ€์ง€ ํ† ํ•‘(1, 2)์„ ๋ง›๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋™์ƒ์€ ๋„ค ๊ฐ€์ง€ ํ† ํ•‘(1, 2, 3, 4)์„ ๋ง›๋ณผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด๋Š” ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋กค์ผ€์ดํฌ์˜ ๋„ค ๋ฒˆ์งธ ํ† ํ•‘(3)๊ณผ ๋‹ค์„ฏ ๋ฒˆ์งธ ํ† ํ•‘(1) ์‚ฌ์ด๋ฅผ ์ž๋ฅด๋ฉด [1, 2, 1, 3], [1, 4, 1, 2]๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ฒ ์ˆ˜๋Š” ์„ธ ๊ฐ€์ง€ ํ† ํ•‘(1, 2, 3)์„, ๋™์ƒ๋„ ์„ธ ๊ฐ€์ง€ ํ† ํ•‘(1, 2, 4)์„ ๋ง›๋ณผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด๋Š” ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ณตํ‰ํ•˜๊ฒŒ ๋กค์ผ€์ดํฌ๋ฅผ ์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋กค์ผ€์ดํฌ๋ฅผ [1, 2, 1, 3, 1], [4, 1, 2]์œผ๋กœ ์ž˜๋ผ๋„ ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๋กค์ผ€์ดํฌ๋ฅผ ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋ˆ„์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋กค์ผ€์ดํฌ์— ์˜ฌ๋ ค์ง„ ํ† ํ•‘๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ ์ •์ˆ˜ ๋ฐฐ์—ด topping์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋กค์ผ€์ดํฌ๋ฅผ ๊ณตํ‰ํ•˜๊ฒŒ ์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ topping์˜ ๊ธธ์ด ≤ 1,000,000
    • 1 ≤ topping์˜ ์›์†Œ ≤ 10,000

์ž…์ถœ๋ ฅ ์˜ˆ

topping result
[1, 2, 1, 3, 1, 4, 1, 2] 2
[1, 2, 3, 1, 4] 0

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋กค์ผ€์ดํฌ๋ฅผ [1, 2, 1, 3], [1, 4, 1, 2] ๋˜๋Š” [1, 2, 1, 3, 1], [4, 1, 2]์™€ ๊ฐ™์ด ์ž๋ฅด๋ฉด ์ฒ ์ˆ˜์™€ ๋™์ƒ์€ ๊ฐ๊ฐ ์„ธ ๊ฐ€์ง€ ํ† ํ•‘์„ ๋ง›๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ณตํ‰ํ•˜๊ฒŒ ๋กค์ผ€์ดํฌ๋ฅผ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์˜ ๋‘ ๊ฐ€์ง€๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋กค์ผ€์ดํฌ๋ฅผ ๊ณตํ‰ํ•˜๊ฒŒ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด ๋ฌธ์ œ๋Š” ์ฝ๋Š” ์ˆœ๊ฐ„, ๋ฐฐ์—ด์„ 2๊ฐœ๋กœ ์ž˜๋ผ์„œ,
    • ์–‘์ชฝ ๋ฐฐ์—ด์„ ์นด์šดํ„ฐ๋กœ ๋ฐ”๊พผ ํ›„, ์นด์šดํ„ฐ์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ์ง€ ํ™•์ธํ•ด์„œ,
    • ์–‘์ชฝ ๋ฐฐ์—ด์˜ ์นด์šดํ„ฐ๊ฐ€ ๊ฐ™์œผ๋ฉด answer์— +1ํ•˜๋Š” ๋ฐฉ์‹์ด๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.
  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ ๊ฐ€์ฆˆ์•„!
from collections import Counter

def solution(topping):
    answer = 0
    for i in range(1, len(topping)):
        ์ฒ ์ˆ˜ = len(Counter(topping[:i]))
        ๋™์ƒ = len(Counter(topping[i:]))
        if ์ฒ ์ˆ˜ == ๋™์ƒ:
            answer += 1
    return answer
  • ๋˜๊ธด ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ์ฅฌ?
  • ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ

  • ์นด์šดํ„ฐ๋ฅผ ์œ ์ง€ํ•œ ์ƒํƒœ์—์„œ
    • ์ธ๋ฑ์Šค๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ
      • ๋™์ƒ๊ฑธ ๋บ์–ด์„œ ์ฒ ์ˆ˜ํ•œํ…Œ ์ฃผ๊ณ ,
        • ๋‘ ์นด์šดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•ด์„œ answer๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
  • ๋˜๋„ค์š”.
from collections import Counter

def solution(topping):
    answer = 0
    ์ฒ ์ˆ˜ = Counter()
    ๋™์ƒ = Counter(topping)
    for i in range(0, len(topping)):
        if topping[i] in ์ฒ ์ˆ˜:
            ์ฒ ์ˆ˜[topping[i]] += 1
        else:
            ์ฒ ์ˆ˜[topping[i]] = 1
        ๋™์ƒ[topping[i]] -= 1
        if ๋™์ƒ[topping[i]] == 0: 
            del ๋™์ƒ[topping[i]]
        if len(์ฒ ์ˆ˜) == len(๋™์ƒ):
            answer += 1
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (12.25ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (92.04ms, 14.8MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (96.31ms, 10.9MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (88.78ms, 11MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (713.67ms, 18.6MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (877.26ms, 51.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1092.56ms, 51.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (877.76ms, 51.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1320.09ms, 50.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1318.05ms, 50.8MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (99.18ms, 11.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (18.00ms, 11.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (1299.89ms, 50.7MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (1338.77ms, 50.7MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (1390.42ms, 50.6MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (999.03ms, 50.8MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (911.40ms, 50.6MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (872.23ms, 51.5MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (977.77ms, 51.5MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (868.84ms, 51.5MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ์–ด์ฐจํ”ผ ์ข…๋ฅ˜๋‹ˆ๊นŒ ์ฒ ์ˆ˜๋Š” ์นด์šดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์„ธํŠธ๋กœ ํ•ด๋„ ๋œ๋‹ค. ๊ตฌํ˜„์€ ๋น„์Šทํ•˜์ง€๋งŒ ์ž๋ฃŒํ˜•์˜ ์ฐจ์ด๋กœ ์†๋„๊ฐ€ 2๋ฐฐ ๋น ๋ฆ„
from collections import Counter

def solution(topping):
    answer = 0
    dic = Counter(topping)
    set_dic = set()
    answer = 0

    for i in topping:
        dic[i] -= 1
        set_dic.add(i)
        if dic[i] == 0:
            dic.pop(i)
        if len(dic) == len(set_dic):
            answer += 1

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (4.56ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (48.64ms, 14.9MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (41.56ms, 10.9MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (42.30ms, 10.7MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (440.97ms, 18.6MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (522.67ms, 51.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (549.98ms, 51.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (522.20ms, 51.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (557.71ms, 50.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (493.76ms, 50.5MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (37.70ms, 10.9MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (4.97ms, 11.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (539.55ms, 50.6MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (522.39ms, 50.5MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (531.04ms, 50.6MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (505.26ms, 50.6MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (511.35ms, 50.5MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (479.03ms, 51.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (477.45ms, 51.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (486.90ms, 51.4MB)
  • ์ผ๋ถ€ ์ผ€์ด์Šค ์ œ์™ธํ•˜๊ณ  ์œ„ ๊ณ ์ˆ˜์˜ ์ฝ”๋“œ ๋ณด๋‹ค ๋” ๋น ๋ฅธ ๋””ํดํŠธ๋”•์…”๋„ˆ๋ฆฌ์™€ ์…‹.
  • ๊ทผ๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ ๋””ํดํŠธ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋น ๋ฅธ๊ฒŒ ์•„๋‹ˆ๋ผ ๋งˆ์ง€๋ง‰ ํ•œ ์ค„ ๋•Œ๋ฌธ์— ๋น ๋ฆ„.
from collections import defaultdict

def solution(topping):
    first, second = defaultdict(int), set()
    count = 0
    for i in topping:
        first[i] = first[i] + 1
    for i in topping:
        first[i] = first[i] - 1
        if first[i] == 0:
            del first[i]
        second.add(i)
        if len(second) == len(first):
            count = count + 1
        if len(second) > len(first):
            break
    return count
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (3.21ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (35.91ms, 14.9MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (42.45ms, 10.9MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (40.15ms, 10.8MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (351.94ms, 18.7MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (381.98ms, 51.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (303.47ms, 51.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (530.29ms, 51.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (124.02ms, 50.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (99.57ms, 50.5MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (10.61ms, 10.8MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (3.92ms, 11.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (523.02ms, 50.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (564.06ms, 50.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (663.46ms, 50.6MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (430.99ms, 50.5MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (426.52ms, 50.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (448.91ms, 51.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (241.21ms, 51.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (197.38ms, 51.2MB)
  • ์œ„ ๋‘ ๊ณ ์ˆ˜์˜ ํ’€์ด๋Š” ๊ตฌํ˜„์€ ๋น„์Šทํ•˜๋‹ค.
  • ์šฐ์„  ์ฝœ๋ ‰์…˜์˜ ๋””ํดํŠธ๋”•๊ณผ ์นด์šดํ„ฐ ์ฐจ์ด์ ์„ ์ง์ ‘ ๋น„๊ตํ•ด๋ณด์ž.
    • ๋””ํดํŠธ๋”•์€ ์ดˆ๊ธฐ๊ฐ’์„ ๋„ฃ์„ ๋•Œ ๊ฐ’์ด ์ด๋ฏธ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 
    • ๊ทผ๋ฐ ๋””ํดํŠธ๋”•์€ ์ฒ˜์Œ ๋งŒ๋“ค ๋•Œ ๊ฐ’์„ ๋„ฃ๋Š” for๋ฌธ์ด ํ•„์š”ํ•˜๋‹ค.
    • ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์ด ์ด๋“์ธ ๊ฑด๋ฐ ์“ธ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ...
    • ํ•˜์ง€๋งŒ ํ•ด๋ณด๋‹ˆ๊นŒ ๋””ํดํŠธ๋”•์œผ๋กœ ๋งŒ๋“ค๊ณ  ํ•œ๋ฒˆ ์ดˆ๊ธฐํ™” ํ•ด์ค€ ํ›„ ํ•˜๋Š”๊ฑฐ๋‚˜ ์นด์šดํ„ฐ๋ฅผ ์จ์„œ ํ•˜๋Š”๊ฑฐ๋‚˜ ๋ณ„ ์ฐจ์ด ์—†์—ˆ๋‹ค.
    • ๋˜ del์„ ์“ฐ๋‚˜ pop์œผ๋กœ ๋นผ๋‚˜ ๋ณ„ ์ฐจ์ด ์—†์—ˆ๋‹ค.
  • ์˜คํžˆ๋ ค ๋งˆ์ง€๋ง‰์— ์ฒ ์ˆ˜๊ฐ€ ๋™์ƒ๋ณด๋‹ค ๋งŽ์•„์ง€๋ฉด ๋” ์ด์ƒ ์ฒดํฌํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋˜ ๋ถ€๋ถ„์ด ์†๋„์˜ ์ฐจ์ด๊ฐ€ ๋‚˜๊ฒŒ ํ–ˆ๋˜ ๋ถ€๋ถ„์ด์—ˆ๋‹ค. ์„ธ์ƒ์€ ๋„“๊ณ  ๊ณ ์ˆ˜๋Š” ๋งŽ๋‹ค.
from collections import Counter, defaultdict

def solution(topping):
    answer = 0
    ์ฒ ์ˆ˜ = set()
    ๋™์ƒ = Counter(topping)
    for i in topping:
        ์ฒ ์ˆ˜.add(i)
        ๋™์ƒ[i] -= 1
        if ๋™์ƒ[i] == 0: 
            ๋™์ƒ.pop(i)
        if len(์ฒ ์ˆ˜) == len(๋™์ƒ):
            answer += 1
        elif len(์ฒ ์ˆ˜) > len(๋™์ƒ):
            break
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (3.00ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (33.64ms, 15.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (40.48ms, 11.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (40.48ms, 11MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (522.91ms, 18.8MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (375.33ms, 51.7MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (305.64ms, 51.8MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (455.42ms, 51.7MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (65.30ms, 50.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (64.02ms, 50.8MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (15.52ms, 11MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (6.64ms, 11.6MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (681.31ms, 50.7MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (637.54ms, 50.8MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (494.96ms, 50.7MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (579.88ms, 50.8MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (533.57ms, 50.7MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (781.31ms, 51.8MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (237.43ms, 51.8MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (201.20ms, 51.8MB)
from collections import Counter, defaultdict

def solution(topping):
    answer = 0
    ์ฒ ์ˆ˜ = set()
    ๋™์ƒ = Counter(topping)
    for i in topping:
        ์ฒ ์ˆ˜.add(i)
        ๋™์ƒ[i] -= 1
        if ๋™์ƒ[i] == 0: 
            del ๋™์ƒ[i]
        if len(์ฒ ์ˆ˜) == len(๋™์ƒ):
            answer += 1
        elif len(์ฒ ์ˆ˜) > len(๋™์ƒ):
            break
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (2.99ms, 10.6MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (32.98ms, 15.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (40.01ms, 11.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (40.99ms, 10.9MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (443.66ms, 18.7MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (370.78ms, 51.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (306.09ms, 51.8MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (466.60ms, 51.7MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (64.06ms, 50.8MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (61.97ms, 50.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (11.07ms, 11MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (5.07ms, 11.6MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (692.39ms, 50.7MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (835.72ms, 50.8MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (726.33ms, 50.8MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (565.27ms, 50.6MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (493.44ms, 50.7MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (486.73ms, 51.8MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (321.10ms, 51.7MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (228.72ms, 51.8MB)
728x90
๋ฐ˜์‘ํ˜•