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
๋ฐ์ํ