๐Ÿง  ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

๋ฌธ์ œ

ํ•จ์ˆ˜ solution์€ ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋ผ. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 118372๋ฉด 873211์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋œ๋‹ค.

  • n์€ 1์ด์ƒ 8000000000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋‚˜์˜ ํ’€์ด

def set_desc(n):
    return int(''.join(
        [str(n) for n in sorted([int(num) for num in str(n)], reverse=True)]))

์‰ฝ๊ฒŒ ํ’€์–ด๋‚ธ ๋“ฏ ํ•˜์ง€๋งŒ ์ž์„ธํžˆ ๋ณด๋ฉด ๋ณต์žกํ•˜๋‹ค. ๋ถˆํ•„์š”ํ•œ ํ˜•๋ณ€ํ™˜์„ ๋งŽ์ด ํ•œ ๋Š๋‚Œ์ด ๋“ ๋‹ค.

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

def set_desc(n):
    ls = list(str(n))
    ls.sort(reverse = True)
    return int("".join(ls))

๋ฌธ์ž์—ด์— ๊ทธ์ € list()๋งŒ ์”Œ์›Œ์ฃผ๋ฉด ์ €์ ˆ๋กœ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ split๋˜์–ด ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ๋‹ค๋Š” ์ โ€ฆ ๊ผญ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space