250x250
Link
๋‚˜์˜ GitHub Contribution ๊ทธ๋ž˜ํ”„
Loading data ...
Notice
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Data Science LAB

[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต level2 (์ด์ง„ ๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ) ๋ณธ๋ฌธ

๐Ÿ“ Coding Test/Programmers

[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต level2 (์ด์ง„ ๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ)

ใ…… ใ…œ ใ…” ใ…‡ 2022. 11. 16. 16:18
728x90

1. ๋ฌธ์ œ ์„ค๋ช…

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ์–ด๋–ค ๋ฌธ์ž์—ด x์— ๋Œ€ํ•œ ์ด์ง„ ๋ณ€ํ™˜์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

  1. x์˜ ๋ชจ๋“  0์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  2. x์˜ ๊ธธ์ด๋ฅผ c๋ผ๊ณ  ํ•˜๋ฉด, x๋ฅผ "c๋ฅผ 2์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ๋ฌธ์ž์—ด"๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, x = "0111010"์ด๋ผ๋ฉด, x์— ์ด์ง„ ๋ณ€ํ™˜์„ ๊ฐ€ํ•˜๋ฉด x = "0111010" -> "1111" -> "100" ์ด ๋ฉ๋‹ˆ๋‹ค.

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. s๊ฐ€ "1"์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ s์— ์ด์ง„ ๋ณ€ํ™˜์„ ๊ฐ€ํ–ˆ์„ ๋•Œ, ์ด์ง„ ๋ณ€ํ™˜์˜ ํšŸ์ˆ˜์™€ ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ์ œ๊ฑฐ๋œ ๋ชจ๋“  0์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

 

2. ์ œํ•œ ์‚ฌํ•ญ

  • s์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 150,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • s์—๋Š” '1'์ด ์ตœ์†Œ ํ•˜๋‚˜ ์ด์ƒ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

3. ๋‚ด ํ’€์ด

def solution(s):
    cnt,zero = 0,str(s).count('0')
    new_s = str(s).count('1')
    
    while new_s != 1:
        cnt += 1
        binary = bin(new_s)
        new_s = binary.count('1')
        zero += binary[2:].count('0')
    return [cnt+1, zero]

 

 

 

 

 

4. ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

def solution(s):
    a, b = 0, 0
    while s != '1':
        a += 1
        num = s.count('1')
        b += len(s) - num
        s = bin(num)[2:]
    return [a, b]

 

 

 

def solution(s):
    cnt, zero = 0, 0
    while True:
        if s == "1":
            return [cnt, zero]
        cnt += 1
        zero += s.count("0") 
        s = bin(s.count("1"))[2:]
728x90
Comments