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

The only one you can truly trust is yourself.

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2๊ฐœ ์ดํ•˜๋กœ ๋‹ค๋ฅธ ๋น„ํŠธ

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

๋ฌธ์ œ๋Š” ์‰ฌ์› ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ 2๊ฐœ ๋ฐœ์ƒํ•œ๋‹ค.

  • ์ฒ˜์Œ ์งฏ๋˜ ์ฝ”๋“œ...
  • ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์˜€๊ณ ...
def solution(numbers):
    answer = []
    
    for i in numbers:
        bin_i = bin(i)[2:]
        bin_i_list = list(bin_i)
        j = i
        while True:
            j += 1
            bin_j = bin(j)[2:]
            bin_j_list = list(bin_j)
            if len(bin_i_list) < len(bin_j_list):
                bin_i_list.insert(0, "0")
            count = 0
            for bi,bj in zip(bin_i_list,bin_j_list):
                if bi != bj:
                    count += 1
            if count <= 2:
                break
        answer.append(j)
    return answer
    
    ์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (6.41ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (753.48ms, 22.8MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1.27ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (3.41ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (8.88ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (8.92ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1544.91ms, 23.8MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (911.41ms, 23.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1357.31ms, 23MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๋” ํ’€์–ด์„œ ์จ์•ผํ•˜๋‚˜? ํ•˜๋ฉด์„œ ๊ณ ์ณ๋ดค๋Š”๋ฐ ๋” ๋Š๋ ค์ง. ใ…‹ใ…‹ใ…‹
def solution(numbers):
    answer = []
    
    for i in numbers:
        bin_i = bin(i)[2:]
        bin_i_list = list(bin_i)
        j = i
        while True:
            j += 1
            bin_j = bin(j)[2:]
            bin_j_list = list(bin_j)
            bi_len = len(bin_i_list)
            bj_len = len(bin_j_list)
            if bi_len < bj_len:
                bin_i_list.insert(0, "0")
            count = 0
            for k in reversed(range(bj_len)):
                if bin_i_list[k] != bin_j_list[k]:
                    count += 1
                    if count > 2:
                        continue
            if count <= 2:
                break
        answer.append(j)
    return answer
    
    ์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (7.13ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (909.03ms, 22.8MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.89ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (4.18ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (13.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (8.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2115.68ms, 23.9MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1226.75ms, 23.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1712.45ms, 23.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๊ทธ๋ž˜์„œ ๋‹ค์‹œ ์ฒ˜์Œ์˜ ์†Œ์Šค๋กœ ๋Œ์•„๊ฐ€์„œ, ์ง์ˆ˜์ผ ๋•Œ๋Š” ๋’ค๊ฐ€ 0์ด๋‹ˆ๊นŒ 1 ์ž๋ฆฌ์ˆ˜๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋˜์„œ ๊ทธ๊ฑฐ ์ฒ˜๋ฆฌํ•ด๋ด„.
  • ์•„์ฃผ ์กฐ๊ธˆ ๋นจ๋ผ์กŒ๋„ค? ๊ฐœ๋ฏธ ๋ฐœ๊ฐ€๋ฝ ๋งŒํผ... ใ…ก.ใ…ก;
def solution(numbers):
    answer = []
    
    for i in numbers:
        if i % 2 == 0:
            j = i + 1
        else:
            bin_i = bin(i)[2:]
            bin_i_list = list(bin_i)
            j = i
            while True:
                j += 1
                bin_j = bin(j)[2:]
                bin_j_list = list(bin_j)
                if len(bin_i_list) < len(bin_j_list):
                    bin_i_list.insert(0, "0")
                count = 0
                for bi,bj in zip(bin_i_list,bin_j_list):
                    if bi != bj:
                        count += 1
                if count <= 2:
                    break
        answer.append(j)
    return answer
    
    ์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (2.57ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (673.15ms, 22.9MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.79ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (3.20ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (8.29ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (5.27ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1515.28ms, 23.7MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (917.98ms, 23.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1085.89ms, 22.9MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ์‘... ์•ˆ๋˜์ฅฌ?
  • ํ…Œ์ŠคํŠธ 10๋ฒˆ 11๋ฒˆ์€ ํ™€์ˆ˜๋งŒ ๋„ฃ์–ด๋‘” ๋“ฏ ใ…ก.ใ…ก;
  • ์ด๊ฒŒ ๊ทธ๋ ‡๊ฒŒ ํฐ ์ˆซ์ž๊ตฌ๋‚˜...

  • ๊ทธ๋ž˜์„œ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฐฌ์ฐฌํžˆ ์ฝ์–ด๋ณด๊ธฐ๋กœ ใ…‹ใ…‹ใ…‹

 

๋ฌธ์ œ ์„ค๋ช…

์–‘์˜ ์ •์ˆ˜ x์— ๋Œ€ํ•œ ํ•จ์ˆ˜ f(x)๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • x๋ณด๋‹ค ํฌ๊ณ  x์™€ ๋น„ํŠธ๊ฐ€ 1~2๊ฐœ ๋‹ค๋ฅธ ์ˆ˜๋“ค ์ค‘์—์„œ ์ œ์ผ ์ž‘์€ ์ˆ˜

์˜ˆ๋ฅผ ๋“ค์–ด,

  • f(2) = 3 ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์ด 2๋ณด๋‹ค ํฐ ์ˆ˜๋“ค ์ค‘์—์„œ ๋น„ํŠธ๊ฐ€ ๋‹ค๋ฅธ ์ง€์ ์ด 2๊ฐœ ์ดํ•˜์ด๋ฉด์„œ ์ œ์ผ ์ž‘์€ ์ˆ˜๊ฐ€ 3์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
์ˆ˜ ๋น„ํŠธ ๋‹ค๋ฅธ ๋น„ํŠธ์˜ ๊ฐœ์ˆ˜
2 000...0010  
3 000...0011 1
  • f(7) = 11 ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์ด 7๋ณด๋‹ค ํฐ ์ˆ˜๋“ค ์ค‘์—์„œ ๋น„ํŠธ๊ฐ€ ๋‹ค๋ฅธ ์ง€์ ์ด 2๊ฐœ ์ดํ•˜์ด๋ฉด์„œ ์ œ์ผ ์ž‘์€ ์ˆ˜๊ฐ€ 11์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
์ˆ˜ ๋น„ํŠธ ๋‹ค๋ฅธ ๋น„ํŠธ์˜ ๊ฐœ์ˆ˜
7 000...0111  
8 000...1000 4
9 000...1001 3
10 000...1010 3
11 000...1011 2

์ •์ˆ˜๋“ค์ด ๋‹ด๊ธด ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์˜ ๋ชจ๋“  ์ˆ˜๋“ค์— ๋Œ€ํ•˜์—ฌ ๊ฐ ์ˆ˜์˜ f ๊ฐ’์„ ๋ฐฐ์—ด์— ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ œํ•œ์‚ฌํ•ญ

  • 1 ≤ numbers์˜ ๊ธธ์ด ≤ 100,000
  • 0 ≤ numbers์˜ ๋ชจ๋“  ์ˆ˜ ≤ 1015

์ž…์ถœ๋ ฅ ์˜ˆ
numbers result
[2,7] [3,11]

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

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

 

ํ’€์ด

  • ์ด๊ฑฐ ๋‹ค์‹œ ๋ณด๋‹ˆ ๊ทœ์น™์„ ์ฐพ๋Š” ๋ฌธ์ œ ๊ฐ™๋‹ค.
  • for๋ฌธ์œผ๋กœ ๋ฌดํ•œ์œผ๋กœ ๋Œ๋ฆด๊ฒŒ ์•„๋‹ˆ๋ผ "01"์ธ๊ฑธ ์ฐพ์•„์„œ "10"์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Œ. "11"์ด๋ฉด "100"์ธ๋ฐ... ์ด๋Ÿฌ๋ฉด 3๊ฐœ๊ฐ€ ๋‹ฌ๋ผ์ง€๋‹ˆ๊นŒ 101๋กœ ํ•ด์ค˜์•ผํ•จ.
  • ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋‚˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•œ๋ฒˆ ๋ฝ‘์•„๋ด„.
 0  1      0b0      0b1
 1  2      0b1     0b10
 2  3     0b10     0b11
 3  5     0b11    0b101
 4  5    0b100    0b101
 5  6    0b101    0b110
 6  7    0b110    0b111
 7 11    0b111   0b1011
 8  9   0b1000   0b1001
 9 10   0b1001   0b1010
10 11   0b1010   0b1011
11 13   0b1011   0b1101
12 13   0b1100   0b1101
13 14   0b1101   0b1110
14 15   0b1110   0b1111
15 23   0b1111  0b10111
16 17  0b10000  0b10001
17 18  0b10001  0b10010
18 19  0b10010  0b10011
19 21  0b10011  0b10101
20 21  0b10100  0b10101
21 22  0b10101  0b10110
22 23  0b10110  0b10111
23 27  0b10111  0b11011
24 25  0b11000  0b11001
25 26  0b11001  0b11010
26 27  0b11010  0b11011
27 29  0b11011  0b11101
28 29  0b11100  0b11101
29 30  0b11101  0b11110
30 31  0b11110  0b11111
31 47  0b11111 0b101111
32 33 0b100000 0b100001
33 34 0b100001 0b100010
34 35 0b100010 0b100011
35 37 0b100011 0b100101
36 37 0b100100 0b100101
37 38 0b100101 0b100110
38 39 0b100110 0b100111
39 43 0b100111 0b101011
40 41 0b101000 0b101001
41 42 0b101001 0b101010
42 43 0b101010 0b101011
43 45 0b101011 0b101101
44 45 0b101100 0b101101
45 46 0b101101 0b101110
46 47 0b101110 0b101111
47 55 0b101111 0b110111
48 49 0b110000 0b110001
49 50 0b110001 0b110010
  • ์ง์ˆ˜์ผ ๋•Œ๋Š” +1 ํ•ด์ฃผ๋ฉด ๋˜๊ณ , ํ™€์ˆ˜์ผ ๋•Œ๋„ +1์ด ๋˜๋Š” ์• ๋“ค์ด ์žˆ์Œ. ๊ทผ๋ฐ +1์ด ์•ˆ๋˜๋Š” ์ˆซ์ž๊ฐ€ ์žˆ์Œ.
    • 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, ...
  • ํ ... ๋ญ์ง€? ๋žœ๋ค์ธ๊ฐ€? ์–ด?
    • 3, 7, 11, 15, 19,
    • 23, 27, 31, 35, 39,
    • 43, 47, ...
  • 3,7,11,15,19... ๋ฐ˜๋ณต์ด๋„ค...
  • ๋„์™€์ค˜์š”! ์šธํ”„๋žŒ์•ŒํŒŒ! 4n - 1?

  • ์ €๊ฑธ ์•Œ์•„๋ƒˆ์–ด๋„ ์•ˆ ๋นจ๋ผ์ง ใ…ก.ใ…ก;
def solution(numbers):
    answer = []
    
    for i in numbers:
        if (i + 1) % 4 != 0:
            j = i + 1
        else:
            bin_i = bin(i)[2:]
            bin_i_list = list(bin_i)
            j = i
            while True:
                j += 1
                bin_j = bin(j)[2:]
                bin_j_list = list(bin_j)
                if len(bin_i_list) < len(bin_j_list):
                    bin_i_list.insert(0, "0")
                count = 0
                for bi,bj in zip(bin_i_list,bin_j_list):
                    if bi != bj:
                        count += 1
                if count <= 2:
                    break
            
        #print(i, j, bin(i), bin(j))
        answer.append(j)
    return answer
  • ์—ฌ๊ธฐ์„œ ๊ทœ์น™์„ ์ฐพ์•„์•ผ๋˜๋„ค...
 3  5     0b11    0b101
 7 11    0b111   0b1011
11 13   0b1011   0b1101
15 23   0b1111  0b10111
19 21  0b10011  0b10101

23 27  0b10111  0b11011
27 29  0b11011  0b11101
31 47  0b11111 0b101111
35 37 0b100011 0b100101
39 43 0b100111 0b101011

43 45 0b101011 0b101101
47 55 0b101111 0b110111
  • ์ฐพ์•˜๋‹ค!!! ์ดํ›„๋กœ๋Š” ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ณ  01์„ ์ฐพ์•„์„œ 10์œผ๋กœ ๊ณ ์น˜๊ณ  ๋‹ค์‹œ ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ณ  ์‹ญ์ง„์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋.
def solution(numbers):
    answer = []
    
    for i in numbers:
        if (i + 1) % 4 != 0:
            j = i + 1
        else:
            bin_i = bin(i)[2:]
            bin_i_list = list(bin_i)
            bin_i_list.insert(0, "0")
            bin_i_list_len = len(bin_i_list)
            for i in reversed(range(bin_i_list_len)):
                if bin_i_list[i] == '0':
                    bin_i_list[i] = '1'
                    if i != bin_i_list_len - 1:
                        bin_i_list[i+1] = '0'
                        break
            bin_i = ''.join(bin_i_list)
            j = int(bin_i,2)
        answer.append(j)
    return answer

์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (45.82ms, 23MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.45ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.75ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (64.88ms, 23.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (52.68ms, 22.9MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (50.72ms, 22.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (336.17ms, 23.8MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (330.90ms, 23.8MB)

์™€- ์ ์ˆ˜ ๊ฐœ ๋งŽ์ด์ฃผ๋„ค?

  • ๊ธฐ๋ถ„ ์ข‹๊ฒŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋„ ๋ณด๋Ÿฌ๊ฐ”๋”๋‹ˆ ํ›„๋œ๋œ...
  • ์™œ ์ธ๊ณต์ง€๋Šฅ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ์—‰ ใ… .ใ… 

ํ•ต๊ณ ์ˆ˜์˜ ํ’€์ด๋ฒ•...

def solution(numbers):
    answer = []
    for idx, val in enumerate(numbers):
        answer.append(((val ^ (val+1)) >> 2) +val +1)

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.40ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (33.20ms, 22.8MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (23.40ms, 23.7MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (21.95ms, 23.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (27.19ms, 22.9MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (21.85ms, 25.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (38.45ms, 25.4MB)

๋‚œ ์•„์ง ๋ฉ€์—ˆ๊ตฌ๋‚˜... ใ…ก.ใ…ก;

728x90
๋ฐ˜์‘ํ˜•