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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ณด์„ ์‡ผํ•‘ (2020 ์นด์นด์˜ค ์ธํ„ด์‰ฝ, ์นด์šดํ„ฐ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ)

๐ŸŽฎinspirer9 2023. 2. 28. 14:15
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

[๋ณธ ๋ฌธ์ œ๋Š” ์ •ํ™•์„ฑ๊ณผ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ๊ฐ๊ฐ ์ ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.]

๊ฐœ๋ฐœ์ž ์ถœ์‹ ์œผ๋กœ ์„ธ๊ณ„ ์ตœ๊ณ ์˜ ๊ฐ‘๋ถ€๊ฐ€ ๋œ ์–ดํ”ผ์น˜๋Š” ์ŠคํŠธ๋ ˆ์Šค๋ฅผ ๋ฐ›์„ ๋•Œ๋ฉด ์ด๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์˜คํ”„๋ผ์ธ ๋งค์žฅ์— ์‡ผํ•‘์„ ํ•˜๋Ÿฌ ๊ฐ€๊ณค ํ•ฉ๋‹ˆ๋‹ค.
์–ดํ”ผ์น˜๋Š” ์‡ผํ•‘์„ ํ•  ๋•Œ๋ฉด ๋งค์žฅ ์ง„์—ด๋Œ€์˜ ํŠน์ • ๋ฒ”์œ„์˜ ๋ฌผ๊ฑด๋“ค์„ ๋ชจ๋‘ ์‹น์“ธ์ด ๊ตฌ๋งคํ•˜๋Š” ์Šต๊ด€์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์–ด๋Š ๋‚  ์ŠคํŠธ๋ ˆ์Šค๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ๋ณด์„ ๋งค์žฅ์— ์‡ผํ•‘์„ ํ•˜๋Ÿฌ ๊ฐ„ ์–ดํ”ผ์น˜๋Š” ์ด์ „์ฒ˜๋Ÿผ ์ง„์—ด๋Œ€์˜ ํŠน์ • ๋ฒ”์œ„์˜ ๋ณด์„์„ ๋ชจ๋‘ ๊ตฌ๋งคํ•˜๋˜ ํŠน๋ณ„ํžˆ ์•„๋ž˜ ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.
์ง„์—ด๋œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ณด์„์„ ์ ์–ด๋„ 1๊ฐœ ์ด์ƒ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์„ ์ฐพ์•„์„œ ๊ตฌ๋งค

์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ์ง„์—ด๋Œ€๋Š” 4์ข…๋ฅ˜์˜ ๋ณด์„(RUBY, DIA, EMERALD, SAPPHIRE) 8๊ฐœ๊ฐ€ ์ง„์—ด๋œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ 1 2 3 4 5 6 7 8
๋ณด์„ ์ด๋ฆ„ DIA RUBY RUBY DIA DIA EMERALD SAPPHIRE DIA

์ง„์—ด๋Œ€์˜ 3๋ฒˆ๋ถ€ํ„ฐ 7๋ฒˆ๊นŒ์ง€ 5๊ฐœ์˜ ๋ณด์„์„ ๊ตฌ๋งคํ•˜๋ฉด ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ณด์„์„ ์ ์–ด๋„ ํ•˜๋‚˜ ์ด์ƒ์”ฉ ํฌํ•จํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ง„์—ด๋Œ€์˜ 3, 4, 6, 7๋ฒˆ์˜ ๋ณด์„๋งŒ ๊ตฌ๋งคํ•˜๋Š” ๊ฒƒ์€ ์ค‘๊ฐ„์— ํŠน์ • ๊ตฌ๊ฐ„(5๋ฒˆ)์ด ๋น ์ง€๊ฒŒ ๋˜๋ฏ€๋กœ ์–ดํ”ผ์น˜์˜ ์‡ผํ•‘ ์Šต๊ด€์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ๋ณด์„๋“ค์˜ ์ด๋ฆ„์ด ์ €์žฅ๋œ ๋ฐฐ์—ด gems๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋“  ๋ณด์„์„ ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์„ ์ฐพ์•„์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ์™€ ๋ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ return ํ•˜๋„๋ก ํ•˜๋ฉฐ, ๋งŒ์•ฝ ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ตฌ๊ฐ„์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • gems ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • gems ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” ์ง„์—ด๋Œ€์— ๋‚˜์—ด๋œ ๋ณด์„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • gems ๋ฐฐ์—ด์—๋Š” 1๋ฒˆ ์ง„์—ด๋Œ€๋ถ€ํ„ฐ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ๋ณด์„์ด๋ฆ„์ด ์ฐจ๋ก€๋Œ€๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • gems ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ 10 ์ดํ•˜์ธ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

gems result
["DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"] [3, 7]
["AA", "AB", "AC", "AA", "AC"] [1, 3]
["XYZ", "XYZ", "XYZ"] [1, 1]
["ZZZ", "YYY", "NNNN", "YYY", "BBB"] [1, 5]

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
3์ข…๋ฅ˜์˜ ๋ณด์„(AA, AB, AC)์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์€ [1, 3], [2, 4]๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๊ฐ€ ๋” ์ž‘์€ [1, 3]์„ return ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
1์ข…๋ฅ˜์˜ ๋ณด์„(XYZ)์„ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๊ตฌ๊ฐ„์€ [1, 1], [2, 2], [3, 3]์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์‹œ์ž‘ ์ง„์—ด๋Œ€ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ [1, 1]์„ return ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #4
4์ข…๋ฅ˜์˜ ๋ณด์„(ZZZ, YYY, NNNN, BBB)์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ๊ตฌ๊ฐ„์€ [1, 5]๊ฐ€ ์œ ์ผํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ [1, 5]๋ฅผ return ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

โ€ป ๊ณต์ง€ - 2020๋…„ 7์›” 21์ผ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ๋ฐ”๋กœ ์นด์šดํ„ฐ์— for๋ฌธ ์จ์„œ ํ’€์—ˆ๋Š”๋ฐ ํ…Œ์ผ€ 6,7๋ฒˆ ์‹คํŒจ, ๋ฐ˜์ฏค ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋œฌ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ์—ˆ๋‹คใ…‹ใ…‹ใ…‹
import collections

def solution(gems):
    cnt_gems = collections.Counter(gems)
    unique = len(cnt_gems)
    
    if unique == 1:
        return [1,1]
    
    best = [0,0,100000]
    for l in range(unique, len(gems)):
        for i in range(len(gems)-l+1):
            cnt_unique_gems = collections.Counter(gems[i:i+l])
            if unique == len(cnt_unique_gems):
                if best[2] > l:
                    best = [i+1,i+l,l]
                    break
    
    if best == [0,0,100000]:
        return [1,len(gems)]
    else:
        return [best[0],best[1]]
  • ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋‚˜...
    • ์ตœ์†Œ ํฌ๊ธฐ๋กœ ์ž˜๋ผ์„œ ์ด๋™ํ•˜๋ฉด์„œ ๋จน๊ณ  ์‹ธ๊ณ  ํ•ด์•ผ๋˜๋‚˜?
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.41ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (18.30ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (450.13ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (5066.25ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (7630.45ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (5070.61ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 12 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 13 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 14 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 15 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ์ง€๋ ์ด ํ™๋จน๊ณ  ๋˜ฅ์‹ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ๋Š”๋ฐ ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•˜๊ตฐ... ํšจ์œจ์„ฑ์—์„œ ์†๋„๊ฐ€ ์กฐ๊ธˆ ์•„์‰ฝ๋‹ค.
    • ์•„๋‹ˆ, ์ •์‹๋ช…์นญ์€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ผ๊ณ  ํ•œ๋‹ค.
def solution(gems):
    answer = []
    unique = len(set(gems))
    collected_gems = {}
    head, tail = 0, 0
    
    while head < len(gems):
        try:
            collected_gems[gems[head]] += 1
        except:
            collected_gems[gems[head]] = 1
        
        if len(collected_gems) == unique: # print(unique,tail,head,collected_gems)
            answer.append([tail,head])
            
        while len(collected_gems) == unique and tail <= head - unique:
            tail += 1
            collected_gems[gems[tail-1]] -= 1
            if collected_gems[gems[tail-1]] == 0:
                del collected_gems[gems[tail-1]]
            if len(collected_gems) == unique: # print(unique,tail,head,collected_gems)
                answer.append([tail,head])
        head += 1
    
    answer.sort(key=lambda x:(x[1]-x[0],x[0]))
    
    return [answer[0][0]+1,answer[0][1]+1]
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.56ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.40ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 9.97MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.45ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1.22ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.94ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.87ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (4.57ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (1.38ms, 10.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (5.44ms, 11.1MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (6.33ms, 11.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (6.86ms, 11.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (20.44ms, 14.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (9.97ms, 11.8MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (31.76ms, 16.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (42.48ms, 18MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (41.93ms, 19.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (51.19ms, 20.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (66.10ms, 22.9MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (75.55ms, 24.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (75.80ms, 25.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (33.92ms, 16MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (50.27ms, 20MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (129.32ms, 34.5MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (119.82ms, 34.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๋น ๋ฅด๋„ค... ํ˜ธ์—ฅ... ๋””ํดํŠธ๋”•ํŠธ๋„ ๊ณต๋ถ€ํ•ด์•ผํ•˜๋ ค๋‚˜
from collections import defaultdict

def solution(gems):
    answer = [0, len(gems)]
    left, right = 0, 0
    set_gems_len = len(set(gems))
    dict = defaultdict(int)
    dict[gems[left]] = 1
    while left < len(gems) and right < len(gems):
        if len(dict) == set_gems_len:
            if right - left < answer[1] - answer[0]:
                answer = [left, right]
            dict[gems[left]] -= 1
            if dict[gems[left]] == 0:
                del(dict[gems[left]])
            left += 1
        else:
            right += 1
            if right >= len(gems):
                break
            dict[gems[right]] += 1
    return [answer[0]+1, answer[1]+1]
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.58ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.68ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.20ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (1.58ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (1.27ms, 10.5MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (3.33ms, 10.3MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (4.35ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (5.88ms, 10.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (11.67ms, 11.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (10.05ms, 12MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (21.66ms, 11.9MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (25.31ms, 12.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (30.02ms, 12.7MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (34.54ms, 12.9MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (36.01ms, 13.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (44.17ms, 13.6MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (50.84ms, 14.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (32.92ms, 15.5MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (46.95ms, 16.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (71.37ms, 17MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (75.91ms, 17.8MB)
  • ์นด์šดํ„ฐ ์‚ฌ์šฉ...
    • ์šฐใ…์™€์˜คใ…กใ…œ์šฐ!
    • ๋‚œ ์•„์ง ๋ฉ€์—ˆ๋‹ค.
from collections import Counter

def solution(gems):
    ans = []
    kinds = len(set(gems))
    counter = Counter()

    start = 0
    for end in range(len(gems)):
        counter[gems[end]] += 1
        end += 1

        while len(counter) == kinds:
            counter[gems[start]] -= 1
            if counter[gems[start]] == 0:
                del counter[gems[start]]
            start += 1
            ans.append([start, end])

    return sorted(ans, key = lambda x: (x[1]-x[0], x[0]))[0]
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.94ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.96ms, 10.5MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.58ms, 10.5MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (2.18ms, 10.6MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (1.16ms, 10.5MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (8.78ms, 10.9MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (6.09ms, 11.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (6.37ms, 11.6MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (20.21ms, 14.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (8.59ms, 11.9MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (30.92ms, 16.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (40.99ms, 18.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (40.22ms, 19.8MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (50.86ms, 20.8MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (67.12ms, 23.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (71.15ms, 24.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (73.88ms, 25.5MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (31.28ms, 15.9MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (50.15ms, 20.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (122.80ms, 35.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (115.90ms, 34.7MB)
728x90
๋ฐ˜์‘ํ˜•