Post

๐ŸฆŠ N์œผ๋กœ ํ‘œํ˜„

1. ๋ฌธ์ œ ๋งํฌ

N์œผ๋กœ ํ‘œํ˜„


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def solution(N, number):
    dp = [set([int(str(N)*i)]) for i in range(1, 9)]

    for count in range(8):
        for i in range(count):
            for left in dp[i]:
                for right in dp[count - i - 1]:
                    dp[count].add(left + right)
                    dp[count].add(left - right)
                    dp[count].add(left * right)
                    if right != 0:
                        dp[count].add(left // right)
        if number in dp[count]:
            return count + 1

    return -1
  • ์ •ํ™•์„ฑ

    ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (0.85ms, 10.5MB) ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.4MB) ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (0.05ms, 10.5MB) ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (28.66ms, 11.2MB) ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (16.45ms, 11.1MB) ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (0.19ms, 10.4MB) ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (0.20ms, 10.4MB) ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (24.62ms, 11.3MB) ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.4MB) ์ฑ„์  ๊ฒฐ๊ณผ ์ •ํ™•์„ฑ: 100.0 ํ•ฉ๊ณ„: 100.0 / 100.0


3. ํ•ด์„ค

i ๋ฒˆ์งธ ์—ฐ์‚ฐ โ‡’ j ๋ฒˆ์งธ ์—ฐ์‚ฐ๊ฒฐ๊ณผ์™€ j - i - 1 ๋ฒˆ์งธ ์—ฐ์‚ฐ์„ ๋‹ค์‹œ ์žฌ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ

์ค‘๋ณต์„ ์—†์• ๊ธฐ ์œ„ํ•ด set ์œผ๋กœ ์ œ๊ฑฐํ•˜๊ณ  ๋น„๊ตํ•˜์—ฌ

์ด๋ฒˆ ํšŒ์ฐจ์— ์—ฐ์‚ฐ์„ ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋ฆฌํ„ด

This post is licensed under CC BY 4.0 by the author.