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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ ์ด์ง„ํŠธ๋ฆฌ (ํฌํ™”์ด์ง„ํŠธ๋ฆฌ)

๐ŸŽฎinspirer9 2023. 2. 22. 16:48
728x90
๋ฐ˜์‘ํ˜•

์ด์ง„ํŠธ๋ฆฌ๋Š” ์•„๋Š”๋ฐ, ์ด์ง„ํŠธ๋ฆฌ, ์‚ผ์ง„ํŠธ๋ฆฌ, ๊ท ํ˜•ํŠธ๋ฆฌ๊นŒ์ง€๋Š” ๊ตฌํ˜„ํ•ด๋ณธ ์  ์žˆ์–ด์„œ ์ด๊ฑฐ ์‰ฌ์šด ๋ฌธ์ œ ์•„๋‹ˆ๋ƒ? ํ•˜๊ณ  ํด๋ฆญํ–ˆ๋Š”๋ฐ ํฌํ™”์ด์ง„ํŠธ๋ฆฌ... ๊ฐœ๋…์ด ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ„๋‹ค. ๋ฌธ์ œ๋ฅผ ์ฝ์–ด๋„ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€์„œ ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰...

  • ํฌํ™”์ด์ง„ํŠธ๋ฆฌ. ๊ทธ๋ƒฅ ๋‹ค ์ฑ„์šด ์ด์ง„ํŠธ๋ฆฌ๋‹ค.
  • ๋‹ค ์ฑ„์›Œ์„œ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๋งํฌ๊ฐ€ ๋Š์–ด์ง€๋Š๋ƒ?๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ๊ฐ™๋‹ค.
  • 1, 3(+2), 7(+4), 15(+8), ...
  • ๋Œ€์ถฉ ๊ฐ€์ฆˆ์•„!

๋ฌธ์ œ ์„ค๋ช…

๋‹น์‹ ์€ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.

์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ด์ง„์ˆ˜๋ฅผ ์ €์žฅํ•  ๋นˆ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  2. ์ฃผ์–ด์ง„ ์ด์ง„ํŠธ๋ฆฌ์— ๋”๋ฏธ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฃจํŠธ ๋…ธ๋“œ๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋งŒ๋“ค์–ด์ง„ ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋“ค์„ ๊ฐ€์žฅ ์™ผ์ชฝ ๋…ธ๋“œ๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๊นŒ์ง€, ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. ๋…ธ๋“œ์˜ ๋†’์ด๋Š” ์‚ดํŽด๋ณด๋Š” ์ˆœ์„œ์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  4. ์‚ดํŽด๋ณธ ๋…ธ๋“œ๊ฐ€ ๋”๋ฏธ ๋…ธ๋“œ๋ผ๋ฉด, ๋ฌธ์ž์—ด ๋’ค์— 0์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์‚ดํŽด๋ณธ ๋…ธ๋“œ๊ฐ€ ๋”๋ฏธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ๋ฌธ์ž์—ด ๋’ค์— 1์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  5. ๋ฌธ์ž์—ด์— ์ €์žฅ๋œ ์ด์ง„์ˆ˜๋ฅผ ์‹ญ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด์ง„ํŠธ๋ฆฌ์—์„œ ๋ฆฌํ”„ ๋…ธ๋“œ๊ฐ€ ์•„๋‹Œ ๋…ธ๋“œ๋Š” ์ž์‹ ์˜ ์™ผ์ชฝ ์ž์‹์ด ๋ฃจํŠธ์ธ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ์˜ค๋ฅธ์ชฝ์— ์žˆ์œผ๋ฉฐ, ์ž์‹ ์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด ๋ฃจํŠธ์ธ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ์™ผ์ชฝ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ์ด์ง„ํŠธ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ์ด์ง„ํŠธ๋ฆฌ์— ๋”๋ฏธ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ๋กœ ๋งŒ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋”๋ฏธ ๋…ธ๋“œ๋Š” ์ ์„ ์œผ๋กœ ํ‘œ์‹œํ•˜์˜€๊ณ , ๋…ธ๋“œ ์•ˆ์˜ ์ˆ˜๋Š” ์‚ดํŽด๋ณด๋Š” ์ˆœ์„œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๋…ธ๋“œ๋“ค์„ ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์‚ดํŽด๋ณด๋ฉฐ 0๊ณผ 1์„ ์ƒ์„ฑํ•œ ๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•˜๋ฉด "0111010"์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ด์ง„์ˆ˜๋ฅผ ์‹ญ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 58์ž…๋‹ˆ๋‹ค.

๋‹น์‹ ์€ ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ, ํ•˜๋‚˜์˜ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ•ด๋‹น ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

์ด์ง„ํŠธ๋ฆฌ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์ˆ˜๋ฅผ ๋‹ด์€ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์˜ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ•ด๋‹น ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด 1์„, ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด 0์„ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

numbers result
[7, 42, 5] [1, 1, 0]
[63, 111, 95] [1, 1, 0]

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

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

7์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

42๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5๋Š” ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, [1, 0]์„ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

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

63์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

111์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

95๋Š” ์ด์ง„ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, [1, 1, 0]์„ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ํ…Œ์ผ€๋Š” ๋˜๋Š”๋ฐ... ์ฑ„์ ํ•˜๋ฉด ์‹คํŒจ + ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ
import math

def check_tree(s):
    l_s = len(s)
    if l_s == 1:
        return s
    else:
        center = l_s//2
        if s[center] == '1':
            return check_tree(s[:center]) + s[center] + check_tree(s[center+1:])
        else:
            return '0' * len(s)
    
def solution(numbers):
    answer = []
    for n in numbers:
        s = bin(n)[2:]
        l = len(s)
        h = int(math.log2(l+1) + 0.5) #๋…ธ๋“œ๊ฐ€ n์ผ๋•Œ ํฌํ™”์ด์ง„ ํŠธ๋ฆฌ์˜ ์ตœ์†Œ๋†’์ด๋Š”... log2(n+1)
        new_l = 2 ** h - 1 #ํฌํ™” ์‹œํ‚ค๋ ค๋ฉด n์ด ๋ช‡๊ฐœ ํ•„์š”ํ•œ๊ฐ€...
        
        tree = []
        for i in range(new_l - l): #์ค‘์‹ฌ์œผ๋กœ ์–ด๋””๋กœ ๋งž์ถฐ์•ผ ํ•˜๋Š”๊ฐ€? ์ผ€์ด์Šค๊ฐ€ ๋ช‡๊ฐœ?
            s = s + "0"
        tree.append(s)
        for i in range(new_l - l):
            s = s[-1:]+s[:-1]
            tree.append(s)
        
        _tmp = 0
        for t in tree:
            if t == check_tree(t):
                _tmp = 1
                break
        answer.append(_tmp)
    return answer
    

ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[7, 42, 5]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[1, 1, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	[63, 111, 95]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[1, 1, 0]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ์ด๋Ÿด ๋• ์žฌ๊ท€ํ˜ธ์ถœ ๋ฆฌ๋ฏธํŠธ๋ฅผ ํ•ด์ œํ•ด๋ณธ๋‹ค.
    • ์‹คํŒจ
      • ํŠธ๋ฆฌ์˜ ๋ชจ์–‘์ด ์•„๋‹Œ๊ฑฐ์•ผ?
        • ์ค‘์‹ฌ์„ ๋งž์ถœ ํ•„์š” ์—†์ด... ๊ทธ๋ƒฅ ์•ž์— 0๋งŒ ์ถ”๊ฐ€ํ•ด์„œ ์นธ ์ฑ„์šฐ๊ณ  ์‹คํ–‰ํ•˜๋ฉด ๋˜๋Š”๊ฑด๊ฐ€?
        • ๊ธ€์ฟค ๋’ค์— 0์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‹ค๋ฅธ ์ˆ˜๊ฐ€ ๋˜๋Š”๊ตฌ๋‚˜!!! 
          • ๋…ธ ์ƒ๊ด€...
            • ์‹คํŒจ...
              • ์–ด์ฐŒํ•ด์•ผ ํ•˜๋‚˜.. .์‹คํŒจ
                • ์กฐ๊ธˆ ๋ฐ”๊ฟ”๋ด.. ์‹คํŒจ...
  • ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋„๋ก ํ•˜์ž ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹
    • ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ???
  • ๋ฌธ์ž์—ด ์ž๋ฅด๋Š” ๋ถ€๋ถ„ ์ธ๋ฑ์Šค ์—๋Ÿฌ ๋‚˜๋Š”๊ฑฐ ์ฐพ์•˜๊ณ ...
    • ์–ด์ฐจํ”ผ ๋‹ต์€ ์‹คํŒจ
import math
import sys
limit_number = 15000
sys.setrecursionlimit(limit_number)

def check_tree(s):
    l_s = len(s)
    if l_s == 1:
        return s
    else:
        center = l_s//2
        if s[center] == '1':
            return check_tree(s[:center]) + s[center] + check_tree(s[-center:])
        else:
            return '0' * l_s
    
def solution(numbers):
    answer = []
    
    for n in numbers:
        s = bin(n)[2:]
        l = len(s)
        h = math.floor(math.log2(l+1) + 0.5) #๋…ธ๋“œ๊ฐ€ n์ผ๋•Œ ํฌํ™”์ด์ง„ ํŠธ๋ฆฌ์˜ ์ตœ์†Œ๋†’์ด๋Š”... log2(n+1)
        new_l = 2**h - 1 #ํฌํ™” ์‹œํ‚ค๋ ค๋ฉด n์ด ๋ช‡๊ฐœ ํ•„์š”ํ•œ๊ฐ€...
        ss = '0' * (new_l-l) + s
        
        if ss == check_tree(ss):
            answer.append(1)
        else:
            answer.append(0)
        
    return answer
  • ์•„... ๊ณ ์ณค๋‹ค.
    • ์–ด์ฉ์ง€... ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๊ฐ–๋‹ค์“ด ์ตœ์†Œ ํŠธ๋ฆฌ ๋†’์ด ๊ณต์‹์ด ๋ฌธ์ œ์˜€๋‹ค.
      • h = int((math.log2(l+1)) + 0.5) # ํ‹€๋ฆผ
      • h = int(math.log2(l)) + 1 # ์ด๊ฒŒ ๋งž์Œ
  • ํ†ต๊ณผ...
    • ๋‚œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์งฐ๋Š”๋ฐ,
    • ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๊ฐ’ ๋น„๊ตํ•ด์„œ True, False ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฑธ๋กœ ์งฐ๋‹ค.
import math
    
def solution(numbers):
    answer = []

    def check_tree(s):
        l_s = len(s)
        if l_s <= 1:
            return s
        else:
            center = l_s//2
            if s[center] == '1':
                return check_tree(s[:center]) + s[center] + check_tree(s[-center:])
            else:
                return '0' * l_s
            
    for n in numbers:
        s = bin(n)[2:]
        l = len(s)
        h = int(math.log2(l)) + 1
        new_l = 2**h - 1
        ss = '0' * (new_l-l) + s
        
        if ss == check_tree(ss):
            answer.append(1)
        else:
            answer.append(0)
        
    return answer
  • ์†๋„๋Š” ๋ณ„๋กœ ์•ˆ ์ข‹์€ ์ค„ ์•Œ์•˜๋Š”๋ฐ (๋ณ„๋กœ ๋‚˜์˜์ง€ ์•Š๋‹ค? ๋‹ค๋ฅธ ๋ถ„๋“ค ๊ฑฐ๋ณด๋‹ค ๋น ๋ฅธ ๊ฒฝ์šฐ๋„ ์žˆ๊ณ )
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.50ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.74ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.59ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3.42ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (29.76ms, 11MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (35.81ms, 11.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (32.86ms, 11.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (35.53ms, 11MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (28.53ms, 11MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (20.11ms, 10.7MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (46.41ms, 11.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (66.56ms, 11.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (43.63ms, 11.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (33.30ms, 10.9MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (24.01ms, 10.8MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๋ผ์ธ ์งง์€ ๊ฑด ์†๋„ ๋น„์Šทํ•˜๊ฑฐ๋‚˜ ๋Š๋ฆฌ๊ณ ,
    • ๋ผ์ธ์ด ๊ธธ๋ฉด ๊ฐ€๋…์„ฑ์ด ์•ˆ์ข‹์•„์„œ ๊ทธ๋ƒฅ ๋‚ด ์ฝ”๋“œ๋กœ ๋งŒ์กฑํ•˜๋Š” ๊ฑธ๋กœ...
      • ๊ฐ€๋…์„ฑ ์ข‹์€ ์ฝ”๋“œ
import sys
sys.setrecursionlimit(100000000)
def solution(numbers):
    # ์ด์ง„ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋นˆ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•œ๋‹ค
    # ์ฃผ์–ด์ง„ ์ด์ง„ํŠธ๋ฆฌ์— ๋”๋ฏธ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ๋กœ ๋งŒ๋“ ๋‹ค
    # ๋งŒ๋“ค์–ด์ง„  ํฌใ… ์ด์ง„ํŠธ๋ฆ์˜ ๋…ธ๋“œ๋“ค์„ ๊ฐ€์žฅ ์™ผ์ชฝ ๋…ธ๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๊นŒ์ง€ ์™ผ์ชฝ์— ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์‚ดํŽด๋ณธ๋‹ค
    # ์‚ดํŽด๋ณธ ๋…ธ๋“œ๊ฐ€ ๋”๋ฏธ ๋…ธ๋“œ๋ผ๋ฉด, ๋ฌธ์ž์—ด ๋’ค์— 0์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์‚ดํŽด๋ณธ ๋…ธ๋“œ๊ฐ€ ๋”๋ฏธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ฉด 1์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    # ๋ฌธ์ž์—ด์— ์ €์žฅ๋œ ์ด์ง„์ˆ˜๋ฅผ ์‹ญ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    # ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ 
    # n์ด ์ฃผ์–ด์งˆ ๋•Œ, 
    # ํŠธ๋ฆฌ์˜ ๋†’์ด... 
    heights = [0] * 6

    for i in range(1,7) :
        heights[i-1] = 2 ** i - 1

    def convert(n) :
        n = bin(n)[2:]
        m = len(n)
        h = 0
        for height in heights :
            if height >= m :
                h = height
                break

        # ํ•ด๋‹น ์„œ๋ธŒ ํŠธ๋ฆฌ๊ฐ€ ์„ฑ๋ฆฝํ•˜๋Š๋ƒ ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š๋ƒ
        def check(num) :
            if len(num) == 1 :
                return True
            mid = len(num) // 2
            # ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ๋”๋ฏธ๋…ธ๋“œ๋ผ๋ฉด
            # ๊ทธ ์•„๋ž˜์˜ ๋…ธ๋“œ๋“ค์€ ์ „๋ถ€ ๋”๋ฏธ๋…ธ๋“œ์—ฌ์•ผ ํ•œ๋‹ค.
            # ์™ผ์ชฝ ์ž์‹์˜ ์„ฑ๋ฆฝ์—ฌ๋ถ€
            left = check(num[:mid])
            # ์˜ค๋ฅธ์ชฝ ์ž์‹์˜ ์„ฑ๋ฆฝ์—ฌ๋ถ€
            right = check(num[mid+1:])
            if num[mid] == '0' :
                l = num[:mid]
                r = num[mid+1:]
                return l[mid//2] == '0' and r[mid//2] == '0' and left and right
            # ๋”๋ฏธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด
            else :
                return left and right

        return check(n.zfill(h))

    return list(map(lambda x : int(convert(x)),numbers))
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.39ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2.15ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (6.97ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (60.65ms, 11.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (67.44ms, 11.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (64.22ms, 11.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (65.08ms, 11.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (53.55ms, 10.9MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (42.54ms, 10.9MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (152.05ms, 11.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (131.74ms, 11.1MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (119.02ms, 10.9MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (124.56ms, 11.1MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (69.30ms, 10.5MB)
  • ๋Œ€๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ๋น„๊ต ์•ˆํ•˜๊ณ , ๊ทธ๋ƒฅ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋ฐ”๋กœ False ๋˜์ง€๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
    • ์ƒ๊ฐํ•ด๋ณด๋‹ˆ False ๋˜์ง€๋Š”๊ฑฐ๋‚˜ '0' * len(s) ๋˜์ง€๋Š”๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.
    • ๊ทธ๋ฆฌ๊ณ  10์˜ 15์Šน์„ 2์ง„์ˆ˜๋กœ ํ•ด๋ดค์ž ๋ช‡ ๊ธ€์ž ์•ˆ๋˜๋‹ˆ๊นŒ, ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด๋กœ ๋‹ค์‹œ ๋„˜๊ธฐ๊ณ  ๋น„๊ตํ•ด๋„ ๋ฌธ์ œ ์—†๋‹ค๊ณ  ํŒ๋‹จ๋จ.
  •  ๋‚ด ์ฝ”๋“œ ์ตœ์ข….
import math

def check_tree(s):
    l_s = len(s)
    if l_s <= 1:
        return s
    center = l_s//2
    if s[center] == '1':
        return check_tree(s[:center]) + s[center] + check_tree(s[-center:])
    else:
        return '0' * l_s

def solution(numbers):
    answer = []
            
    for n in numbers:
        s = bin(n)[2:]
        l = len(s)
        new_l = 2**(int(math.log2(l)) + 1) - 1
        s = '0' * (new_l-l) + s
        if s == check_tree(s):
            answer.append(1)
        else:
            answer.append(0)
        
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.13ms, 10MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.76ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (4.10ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (33.47ms, 10.9MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (38.04ms, 11.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (32.66ms, 11.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (52.37ms, 11.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (49.03ms, 11.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (22.16ms, 10.7MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (64.52ms, 11.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (54.35ms, 11.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (64.12ms, 11.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (48.87ms, 10.9MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (20.43ms, 10.8MB)
728x90
๋ฐ˜์‘ํ˜•