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

The only one you can truly trust is yourself.

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

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

๐ŸŽฎinspirer9 2023. 2. 20. 21:52
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์„ ํ–‰ ์Šคํ‚ฌ์ด๋ž€ ์–ด๋–ค ์Šคํ‚ฌ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๋จผ์ € ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ์Šคํ‚ฌ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์„ ํ–‰ ์Šคํ‚ฌ ์ˆœ์„œ๊ฐ€ ์ŠคํŒŒํฌ → ๋ผ์ดํŠธ๋‹ ๋ณผํŠธ → ์ฌ๋”์ผ๋•Œ, ์ฌ๋”๋ฅผ ๋ฐฐ์šฐ๋ ค๋ฉด ๋จผ์ € ๋ผ์ดํŠธ๋‹ ๋ณผํŠธ๋ฅผ ๋ฐฐ์›Œ์•ผ ํ•˜๊ณ , ๋ผ์ดํŠธ๋‹ ๋ณผํŠธ๋ฅผ ๋ฐฐ์šฐ๋ ค๋ฉด ๋จผ์ € ์ŠคํŒŒํฌ๋ฅผ ๋ฐฐ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์ˆœ์„œ์— ์—†๋Š” ๋‹ค๋ฅธ ์Šคํ‚ฌ(ํž๋ง ๋“ฑ)์€ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ŠคํŒŒํฌ → ํž๋ง → ๋ผ์ดํŠธ๋‹ ๋ณผํŠธ → ์ฌ๋”์™€ ๊ฐ™์€ ์Šคํ‚ฌํŠธ๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์ฌ๋” → ์ŠคํŒŒํฌ๋‚˜ ๋ผ์ดํŠธ๋‹ ๋ณผํŠธ → ์ŠคํŒŒํฌ → ํž๋ง → ์ฌ๋”์™€ ๊ฐ™์€ ์Šคํ‚ฌํŠธ๋ฆฌ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์„ ํ–‰ ์Šคํ‚ฌ ์ˆœ์„œ skill๊ณผ ์œ ์ €๋“ค์ด ๋งŒ๋“  ์Šคํ‚ฌํŠธ๋ฆฌ1๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด skill_trees๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๋Šฅํ•œ ์Šคํ‚ฌํŠธ๋ฆฌ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • ์Šคํ‚ฌ์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๋ฉฐ, ๋ชจ๋“  ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์Šคํ‚ฌ ์ˆœ์„œ์™€ ์Šคํ‚ฌํŠธ๋ฆฌ๋Š” ๋ฌธ์ž์—ด๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, C → B → D ๋ผ๋ฉด "CBD"๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค
  • ์„ ํ–‰ ์Šคํ‚ฌ ์ˆœ์„œ skill์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 26 ์ดํ•˜์ด๋ฉฐ, ์Šคํ‚ฌ์€ ์ค‘๋ณตํ•ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • skill_trees๋Š” ๊ธธ์ด 1 ์ด์ƒ 20 ์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • skill_trees์˜ ์›์†Œ๋Š” ์Šคํ‚ฌ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • skill_trees์˜ ์›์†Œ๋Š” ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ 26 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ด๋ฉฐ, ์Šคํ‚ฌ์ด ์ค‘๋ณตํ•ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

skill skill_trees return
"CBD" ["BACDE", "CBADF", "AECB", "BDA"] 2

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

  • "BACDE": B ์Šคํ‚ฌ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— C ์Šคํ‚ฌ์„ ๋จผ์ € ๋ฐฐ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ๊ฐ€๋Šฅํ•œ ์Šคํ‚ฌํŠธ๋ฆฝ๋‹ˆ๋‹ค.
  • "CBADF": ๊ฐ€๋Šฅํ•œ ์Šคํ‚ฌํŠธ๋ฆฌ์ž…๋‹ˆ๋‹ค.
  • "AECB": ๊ฐ€๋Šฅํ•œ ์Šคํ‚ฌํŠธ๋ฆฌ์ž…๋‹ˆ๋‹ค.
  • "BDA": B ์Šคํ‚ฌ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— C ์Šคํ‚ฌ์„ ๋จผ์ € ๋ฐฐ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ๊ฐ€๋Šฅํ•œ ์Šคํ‚ฌํŠธ๋ฆฌ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด๊ฑฐ ๋„ˆ๋ฌด ์‰ฝ์ง€ ์•Š๋ƒ?
def solution(skill, skill_trees):
    answer = 0
    lst1 = list(skill)
    lst2 = list(skill_trees)
    for st in skill_trees:
        lst2 = list(st)
        lst2 = [i for i in lst2 if i in lst1]
        is_passed = False
        for i in range(len(lst2)):
            if lst2[i] == lst1[i]:
                is_passed = True
                pass
            else:
                is_passed = False
                break
        if is_passed == True:
            answer += 1
    return answer
  • ใ…‡ใ…‡ ์‰ฝ์ง€์•Š์•„... ๋ญ๊ฐ€ ํ‹€๋ฆฐ๊ฑฐ์•ผ?
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.06ms, 10MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	์‹คํŒจ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	์‹คํŒจ (0.02ms, 10.1MB)
  • ์•„! ์ค‘๊ฐ„์— ๋ญ ์žˆ์œผ๋ฉด ์•ˆ๋˜๋Š”๊ฑฐ์•ผ???
  • CBD๊ฐ€ ๋ผ๋‹ˆ๊นŒ, CB, BD๋Š” ๋˜๊ณ , CAB๋Š” ์•ˆ๋˜๋Š”๊ฑด๊ฐ€? ์ฐธ๋‚˜... ใ…ก.ใ…ก`
def solution(skill, skill_trees):
    answer = 0
    lst1 = list(skill)
    for s_tree in skill_trees:
        s_list = list(s_tree)
        lst2 = [(s_list[i],i) for i in range(len(s_list)) if s_list[i] in lst1]
        lst2 = dict(lst2)
        check = -1
        for i in lst2.values():
            if check == -1:
                check = i
            else:
                if check+1 == i:
                    pass
                else:
                    check = -1
                    break
        if check != -1:
            answer += 1
    return answer
  • ์•„๋‹ˆ์•ผ... ๋ฌธ์ œ ์ฝ์–ด๋ณด๋ฉด ๋งจ ์ฒ˜์Œ ์ง  ๊ฑฐ ๋งž์•„. ๊ทผ๋ฐ ์™œ ์‹คํŒจ ๋œจ์ง€?
def solution(skill, skill_trees):
    answer = 0
    lst1 = list(skill)
    print(lst1) # ์ด ์ˆœ์„œ๋Œ€๋กœ ๋˜์–ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•ด์•ผ ํ•จ
    for skill_tree in skill_trees:
        lst2 = list(skill_tree)
        lst2 = [_ for _ in lst2 if _ in lst1]
        is_passed = False
        for i in range(len(lst2)):
            if lst2[i] == lst1[i]: #๊ฐ™์œผ๋ฉด?
                print(lst1, lst2, i, "๋ฒˆ์งธ ๊ฐ™์Œ") # ๋น„๊ต
                is_passed = True
            else:
                print(lst1, lst2, i, "๋ฒˆ์งธ ๋‹ค๋ฆ„") # ๋น„๊ต
                is_passed = False
                break
        if is_passed == True:
            answer += 1
    return answer
  • ํ˜น์‹œ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ˆœ์„œ๊ฐ€ ํ‹€์–ด์ง„๋‹ค๊ฑฐ๋‚˜ ํ•˜๋‚˜?
    • lst2 = list(skill_tree)
    • lst2 = [_ for _ in lst2 if _ in lst1]
  • ๊ทธ๋ž˜์„œ append๋กœ ๋ฐ”๊ฟ”๋ณด์•˜์œผ๋‚˜ ๋˜‘๊ฐ™์€ ์—๋Ÿฌ ๋ฐœ์ƒ
    • lst2 = []
    • for l in skill_tree:
    •     if l in lst1:
    •        lst2.append(l)
  • ์•„ ๋ญ์ง€? ์ด์ƒํ•ด ์ด์ƒํ•ด ์ด์ƒํ•ด.
    • ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ๊ฑด๊ฐ€?
  • ์ข€ ๊ณ ์ณ๋ด„. ๊ทผ๋ฐ ํ†ต๊ณผ๋จ.
    • ๋ญ์•ผ? ์™œ ์ด๋ž˜???
    • ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜ for๋ฌธ ๋Œ๋ ค์„œ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋˜‘๊ฐ™๋‹ค.
    • ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒŒ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.
    • ๋ญ”๊ฐ€ ๊ผฌ์ด๋‚˜๋ด„?
def solution(skill, skill_trees):
    answer = 0
    lst1 = list(skill)
    for skill_tree in skill_trees:
        lst2 = [_ for _ in skill_tree if _ in lst1]
        is_passed = True
        for i in range(len(lst2)):
            if lst2[i] != lst1[i]:
                is_passed = False
                break
        if is_passed == True:
            answer += 1
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด
    • ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊ฟ€ ํ•„์š” ์—†์ด, ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด๋กœ ํ™•์ธํ•ด๋„ ๋˜๋‚˜๋ณด๋‹ค.
def solution(skill, skill_trees):
    answer = 0

    for skills in skill_trees:
        skill_list = list(skill)

        for s in skills:
            if s in skill:
                if s != skill_list.pop(0):
                    break
        else:
            answer += 1

    return answer
    
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
  • ์ด๊ฑฐ๋„ ๋น„์Šท
def solution(skill,skill_tree):
    answer=0
    for i in skill_tree:
        skillist=''
        for z in i:
            if z in skill:
                skillist+=z
        if skillist==skill[0:len(skillist)]:
            answer+=1
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ์•„ ๊ทธ๋ฆฌ๊ณ  ๋‚ด ์ฝ”๋“œ๊ฐ€ ํ†ต๊ณผ ์•ˆ๋œ ์ด์œ ๋ฅผ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์ด ๋ถ€๋ถ„์ธ๋ฐ...
    • lst2 = list(skill_tree) #์Šคํ‚ฌํŠธ๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฑธ ์ฐธ์กฐํ•˜๋Š” lst2๋ž€ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ฌ.
    • lst2 = [_ for _ in lst2 if _ in lst1] #๊ทธ ๋ณ€์ˆ˜ lst2์—์„œ ์›์†Œ๋“ค ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ฒดํฌํ•ด์„œ lst1์— ์—†๋Š” ๊ฑด ์ง€์›€.
    • lst2๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ์ฐธ์กฐ๋˜๋ฉด์„œ ๊ผฌ์ด๋Š” ๋ถ€๋ถ„? ํ™•์‹ค์ด ์ด ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ์‹คํŒจ๊ฐ€ ๋‚˜๊ณ  ์žˆ์—ˆ๋‹ค.
      • ์™œ ๊ทธ๋Ÿฐ์ง€๋Š” ๋ชฐ?๋ฃจ
728x90
๋ฐ˜์‘ํ˜•