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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์šฐ๋ฐ•์ˆ˜์—ด ์ •์ ๋ถ„

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

๋ฌธ์ œ ์„ค๋ช…

์ฝœ๋ผ์ธ  ์ถ”์ธก์ด๋ž€ ๋กœํƒ€๋ฅด ์ฝœ๋ผ์ธ (Lothar Collatz)๊ฐ€ 1937๋…„์— ์ œ๊ธฐํ•œ ์ถ”์ธก์œผ๋กœ ๋ชจ๋“  ์ž์—ฐ์ˆ˜ n์— ๋Œ€ํ•ด ๋‹ค์Œ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋ฉด ํ•ญ์ƒ 1๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ์ถ”์ธก์ž…๋‹ˆ๋‹ค.

1-1. ์ž…๋ ฅ๋œ ์ˆ˜๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด 2๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
1-2. ์ž…๋ ฅ๋œ ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋ผ๋ฉด 3์„ ๊ณฑํ•˜๊ณ  1์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.
2.๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ์ˆ˜๊ฐ€ 1๋ณด๋‹ค ํฌ๋‹ค๋ฉด 1๋ฒˆ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ฃผ์–ด์ง„ ์ˆ˜๊ฐ€ 5 ๋ผ๋ฉด 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒2 ⇒ 1 ์ด๋˜์–ด ์ด 5๋ฒˆ๋งŒ์— 1์ด ๋ฉ๋‹ˆ๋‹ค.

์ˆ˜๊ฐ€ ์ปค์กŒ๋‹ค ์ž‘์•„์ง€๊ธฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๋ชจ์Šต์ด ๋น„๊ตฌ๋ฆ„์—์„œ ๋น—๋ฐฉ์šธ์ด ์˜ค๋ฅด๋ฝ๋‚ด๋ฆฌ๋ฝํ•˜๋ฉฐ ์šฐ๋ฐ•์ด ๋˜๋Š” ๋ชจ์Šต๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ํ•˜์—ฌ ์šฐ๋ฐ•์ˆ˜ ๋˜๋Š” ์šฐ๋ฐ•์ˆ˜์—ด๋กœ ๋ถˆ๋ฆฌ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์ด ์ถ”์ธก์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ์ฆ๋ช…๋˜์ง€ ์•Š์•˜์ง€๋งŒ ์•ฝ 1ํ•ด๊นŒ์ง€์˜ ์ˆ˜์—์„œ ๋ฐ˜๋ก€๊ฐ€ ์—†์Œ์ด ๋ฐํ˜€์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์€์ง€๋Š” ์šฐ๋ฐ•์ˆ˜์—ด์„ ์ขŒํ‘œ ํ‰๋ฉด ์œ„์— ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ดˆํ•ญ์ด K์ธ ์šฐ๋ฐ•์ˆ˜์—ด์ด ์žˆ๋‹ค๋ฉด, x = 0์ผ๋•Œ y = K์ด๊ณ  ๋‹ค์Œ ์šฐ๋ฐ•์ˆ˜๋Š” x = 1์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ์šฐ๋ฐ•์ˆ˜๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ ์ ๋“ค์„ ์ฐ๊ณ  ์ธ์ ‘ํ•œ ์ ๋“ค๋ผ๋ฆฌ ์ง์„ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์€์ง€๋Š” ์ด๋ ‡๊ฒŒ ๋งŒ๋“  ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๋ฅผ ์ •์ ๋ถ„ ํ•ด๋ณด๊ณ  ์‹ถ์–ด์กŒ์Šต๋‹ˆ๋‹ค. x์— ๋Œ€ํ•œ ์–ด๋–ค ๋ฒ”์œ„ [a, b]๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋ฉด ์ด ๋ฒ”์œ„์— ๋Œ€ํ•œ ์ •์ ๋ถ„ ๊ฒฐ๊ณผ๋Š” ๊บพ์€์„  ๊ทธ๋ž˜ํ”„์™€ x = a, x = b, y = 0 ์œผ๋กœ ๋‘˜๋Ÿฌ ์Œ“์ธ ๊ณต๊ฐ„์˜ ๋ฉด์ ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์€์ง€๋Š” ์ด๊ฒƒ์„ ์šฐ๋ฐ•์ˆ˜์—ด ์ •์ ๋ถ„์ด๋ผ๊ณ  ์ •์˜ํ•˜์˜€๊ณ  ๋‹ค์–‘ํ•œ ๊ตฌ๊ฐ„์— ๋Œ€ํ•ด์„œ ์šฐ๋ฐ•์ˆ˜์—ด ์ •์ ๋ถ„์„ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋‹จ, ์šฐ๋ฐ•์ˆ˜์—ด ๊ทธ๋ž˜ํ”„์˜ ๊ฐ€๋กœ์ถ• ๊ธธ์ด๋ฅผ ๋ฏธ๋ฆฌ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์€ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜, ๊ตฌ๊ฐ„์˜ ๋์€ ์–‘์ด ์•„๋‹Œ ์ •์ˆ˜๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ๊ฐ ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์ ๊ณผ ๋๋‚˜๋Š” ์ ์˜ x์ขŒํ‘œ์— ๋Œ€ํ•œ ์ƒ๋Œ€์ ์ธ ์˜คํ”„์…‹์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 5๋ฅผ ์ดˆํ•ญ์œผ๋กœ ํ•˜๋Š” ์šฐ๋ฐ•์ˆ˜์—ด์€ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ขŒํ‘œ ํ‰๋ฉด์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด (0, 5), (1, 16), (2, 8), (3, 4), (4, 2), (5, 1) ์— ์ ์ด ์ฐํžˆ๊ณ  ์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋ฉด ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด๋ฅผ [0,0] ๊ตฌ๊ฐ„์— ๋Œ€ํ•ด ์ •์ ๋ถ„ ํ•œ๋‹ค๋ฉด ์ „์ฒด ๊ตฌ๊ฐ„์— ๋Œ€ํ•œ ์ •์ ๋ถ„์ด๋ฉฐ, [1,-2] ๊ตฌ๊ฐ„์— ๋Œ€ํ•ด ์ •์ ๋ถ„ ํ•œ๋‹ค๋ฉด 1 ≤ x ≤ 3์ธ ๊ตฌ๊ฐ„์— ๋Œ€ํ•œ ์ •์ ๋ถ„์ž…๋‹ˆ๋‹ค.

์šฐ๋ฐ•์ˆ˜์˜ ์ดˆํ•ญ k์™€, ์ •์ ๋ถ„์„ ๊ตฌํ•˜๋Š” ๊ตฌ๊ฐ„๋“ค์˜ ๋ชฉ๋ก ranges๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์ •์ ๋ถ„์˜ ๊ฒฐ๊ณผ ๋ชฉ๋ก์„ return ํ•˜๋„๋ก solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ์ฃผ์–ด์ง„ ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์ ์ด ๋์ ๋ณด๋‹ค ์ปค์„œ ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ตฌ๊ฐ„์ด ์ฃผ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋•Œ์˜ ์ •์ ๋ถ„ ๊ฒฐ๊ณผ๋Š” -1๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • 2 ≤ k ≤ 10,000
  • 1 ≤ ranges์˜ ๊ธธ์ด ≤ 10,000
    • ranges์˜ ์›์†Œ๋Š” [a, b] ํ˜•์‹์ด๋ฉฐ 0 ≤ a < 200, -200 < b ≤ 0 ์ž…๋‹ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•ด ์ •์ ๋ถ„์˜ ๊ฒฐ๊ณผ๋Š” 227 ์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ณธ ๋ฌธ์ œ๋Š” ์ •๋‹ต์— ์‹ค์ˆ˜ํ˜•์ด ํฌํ•จ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ์˜ ์†Œ์ˆ˜ ๋ถ€๋ถ„ .0์ด ์ฝ”๋“œ ์‹คํ–‰ ๋ฒ„ํŠผ ํด๋ฆญ ํ›„ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฐ๊ด๊ฐ’, ๊ธฐ๋Œ“๊ฐ’ ํ‘œ์‹œ์™€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

k ranges result
5 [[0,0],[0,-1],[2,-3],[3,-3]] [33.0,31.5,0.0,-1.0]

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

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

5๋กœ ์‹œ์ž‘ํ•˜๋Š” ์šฐ๋ฐ•์ˆ˜์—ด์€ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1 ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„์—์„œ ๊บพ์ด๋Š” ์ง€์ ์„ ๊ฒฝ๊ณ„๋กœ 5๊ฐœ์˜ ๊ตฌ์—ญ์œผ๋กœ ๋‚˜๋ˆ ๋ณด๋ฉด ๊ฐ๊ฐ์˜ ๊ตฌ๊ฐ„ ๋„“์ด๋Š” 10.5, 12, 6, 3, 1.5 ์ž…๋‹ˆ๋‹ค.

๋ฌด์ง€์„ฑ ํ’€์ด

  • ์–ด๋ผ? ํ•œ๋ฒˆ์— ํ†ต๊ณผ๋œ ๊ฑด ์ฒ˜์Œ์ธ๋ฐ?
def solution(k, ranges):
    answer = []
    k_list = [k]
    
    while k != 1:
        if k % 2 == 0:
            k = k // 2
        else:
            k = k * 3 + 1
        k_list.append(k)

    k_range = len(k_list)
    
    def hailstorm_memoization(n,memo={}):
        if n in memo:
            return memo[n]
        else:
            memo[n] = min(k_list[n],k_list[n+1]) + abs(k_list[n] - k_list[n+1])/2
        return memo[n]
        
    for i in range(len(ranges)):
        a = ranges[i][0]
        b = ranges[i][1] + k_range - 1
        _tmp = 0
        if a < b:
            for i in range(a, b):
                _tmp += hailstorm_memoization(i)
        elif a > b:
            _tmp = -1
        answer.append(_tmp)
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (1.29ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (38.30ms, 12.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (2.50ms, 10.6MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.98ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (7.37ms, 10.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (40.97ms, 12.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (45.02ms, 12.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (11.47ms, 10.8MB)
  • ๊ทผ๋ฐ ๋‹ค๋ฅธ ๊ณ ์ˆ˜๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ...
  • ํ—? ๋ฉ”๋ชจ ์•ˆํ•ด๋„ ๋˜๋‚˜๋ด?
  • ์•„... ๊ทธ ์•ž์—๊นŒ์ง€ ํ•ฉ์„ ๋‹ค ๋”ํ•ด๋†“๊ณ , ๋นผ๋Š”๊ตฌ๋‚˜? ๋Œ€๋ฐ•!
from itertools import accumulate

def collas(k):
    coord = []
    while k != 1:
        coord.append(k)
        if k % 2:
            k = k * 3 + 1
        else:
            k //= 2
    coord.append(k)
    return coord

def solution(k, ranges):
    coord = collas(k)
    area = []
    for i in range(len(coord) - 1):
        area.append((coord[i] + coord[i+1])/2)
    area = [0] + list(accumulate(area))
    n = len(area) - 1
    result = []
    for i, j in ranges:
        k = n + j
        if i > k:
            result.append(-1)
        elif i == k:
            result.append(0)
        else:
            result.append(area[k] - area[i])
    return result
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1.43ms, 12.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.45ms, 10.6MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.49ms, 12MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (2.64ms, 12.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.97ms, 11MB)
def solution(k, ranges):
    answer = []
    arr = [k]
    while k > 1:
        if not k % 2: k //= 2
        else: k = k*3+1
        arr.append(k)
    area = [0]
    for i in range(len(arr)-1):
        area.append(area[-1]+(arr[i]+arr[i+1])/2)
    for a, b in ranges:
        if a >= len(area) or b-1 < -len(area): answer.append(-1)
        elif area[b-1]-area[a] < 0: answer.append(-1)
        else: answer.append(area[b-1]-area[a])
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (3.45ms, 12.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.73ms, 10.5MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.27ms, 10MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1.05ms, 10.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (5.39ms, 11.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (5.75ms, 12.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1.22ms, 10.9MB)
  • ๋‚ด ์ฝ”๋“œ์—์„œ ๋ฉ”๋ชจ์ด์ œ์ด์…˜๋งŒ ๋นผ๋ฉด?
  • for๋ฌธ์ด ํ•˜๋‚˜ ์žˆ์–ด์„œ ์•ˆ ๋นจ๋ผ์ง.
def solution(k, ranges):
    answer = []
    k_list = [k]
    
    while k != 1:
        if k % 2 == 0:
            k = k // 2
        else:
            k = k * 3 + 1
        k_list.append(k)

    for n in range(len(k_list)-1):
        k_list[n] = (k_list[n] + k_list[n+1])/2 # ์ด๊ฑธ ์™œ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ์„๊นŒ...
    
    lk = len(k_list) - 1
    for a, b in ranges:
        b += lk
        if a < b:
            _tmp = 0
            for i in range(a, b): # ์ด ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ๋Š๋ ค์ง.
                _tmp += k_list[i]
            answer.append(_tmp)
        elif a > b:
            answer.append(-1)
        if a == b:
            answer.append(0)
    return answer
  • ๊ทธ๋ž˜๋„ ์•„๊นŒ ๊ฒƒ ๋ณด๋‹ค ๋น ๋ฅด๊ธด ํ•˜๋‹ค.
  • ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๊ฐ€ ์—†์œผ๋ฉด ๊ทธ๋ƒฅ for๋ฌธ ์ ๊ฒŒ ๋Œ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ ์งœ๋Š”๊ฒŒ ์ œ์ผ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.40ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (15.82ms, 12.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.67ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (3.17ms, 10.6MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (18.05ms, 12MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (18.10ms, 12.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (4.23ms, 10.8MB)

 ์ˆ˜ํฌ์ž์˜ ์ •์ ๋ถ„ ใ… .ใ… 

728x90
๋ฐ˜์‘ํ˜•