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

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ (๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ, DP)

๐ŸŽฎinspirer9 2023. 3. 6. 12:34
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์•ž๋’ค๋ฅผ ๋’ค์ง‘์–ด๋„ ๋˜‘๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํŒฐ๋ฆฐ๋“œ๋กฌ(palindrome)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, s์˜ ๋ถ€๋ถ„๋ฌธ์ž์—ด(Substring)์ค‘ ๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ๋“ค๋ฉด, ๋ฌธ์ž์—ด s๊ฐ€ "abcdcba"์ด๋ฉด 7์„ returnํ•˜๊ณ  "abacde"์ด๋ฉด 3์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด : 2,500 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜
  • ๋ฌธ์ž์—ด s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ

์ž…์ถœ๋ ฅ ์˜ˆ

s answer
"abcdcba" 7
"abacde" 3

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
4๋ฒˆ์งธ์ž๋ฆฌ 'd'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด s ์ „์ฒด๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ๋˜๋ฏ€๋กœ 7์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
2๋ฒˆ์งธ์ž๋ฆฌ 'b'๋ฅผ ๊ธฐ์ค€์œผ๋กœ "aba"๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ๋˜๋ฏ€๋กœ 3์„ returnํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ๋จธ๋ฆฌ๊ฐ€ ์•ˆ ๋Œ์•„๊ฐ€๊ณ  ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉํ•˜๊ณ  ์‹ถ์–ด์„œ ๋ฌธ์ œ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฑธ ๊ณจ๋ผ๋ณด์•˜๋‹ค.
    • ๋ญ”๊ฐ€ ์ดˆ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ ๊ฐ™์•„์„œ ๋ฌด์ง€์„ฑ์œผ๋กœ ๋„์ „...
    • ์ผ๋ถ€ ํ…Œ์ผ€๋งŒ ํ†ต๊ณผํ•จ... ๋ฐ˜์ •๋„๋งŒ ๋งž๋„ค? ์™œ ์ด๋ž˜...ใ… .ใ… 
      • ์ ์  ๊ดด๋กœ์›Œ์ง... ๋ฌด์ง€์„ฑ์œผ๋กœ ์ฝ”๋”ฉํ•˜๊ณ  ์‹ถ์—‰...
  • ์‚ฝ์งˆ์Šค...
def solution(s):
    answer = 0            

    i,j=0,0
    s_l = list(s)
    s_l = [ord(i) for i in s]
    s_r = s_l.copy()
    s_r.reverse()
    
    # print(s_l)
    # print(s_r)
    
    count = 0
    for i in range(len_s):
        for j in range(len_s-i):
            if s_l[i+j] == s_r[j]:
                count += 1
            else:
                if answer < count:
                    word_a = s_r[j-count:j]
                    word_b = word_a[::-1]
                    print(word_a, word_b)
                    if word_a == word_b:
                        answer = max(answer, count)
                count = 0
    if answer < count:
        word_a = s_r[j-count:j]
        word_b = word_a[::-1]
        print(word_a, word_b)
        if word_a == word_b:
            answer = max(answer, count)
    count = 0

    for i in range(len_s):
        for j in range(len_s-i):
            if s_l[j] == s_r[i+j]:
                count += 1
            else:
                if answer < count:
                    word_a = s_l[j-count:j]
                    word_b = word_a[::-1]
                    print(word_a, word_b)
                    if word_a == word_b:
                        answer = max(answer, count)
                count = 0
    if answer < count:
        word_a = s_l[j-count:j]
        word_b = word_a[::-1]
        print(word_a, word_b)
        if word_a == word_b:
            answer = max(answer, count)
    count = 0

    return answer
  • ๋‘๋ฒˆ์งธ ์‚ฝ์งˆ์Šค
def solution(s):
    answer = 1          
    count = 1
    set_s = list(set(s))
    word_indexes = {}
    
    if len(set_s) == 1:
        return len(s)
    
    while True:
        for c in s:
            begin = s.index(c)
            end   = s.rindex(c)
            if begin != end:
                word_indexes[c] = (begin, end)
        
        if len(word_indexes) == 0:
            break

        b2 = len(s)
        e2 = -1
        for k,v in list(word_indexes.items()):
            b,e = v[0],v[1]
            if b < b2:
                b2 = b
            if e2 < e:
                e2 = e
            if e-b+1 > count:
                count = e-b+1
                word1 = s[b:e+1]
                word2 = word1[::-1]
                if word1 == word2:
                    answer = max(answer, count)
        
        word_indexes.clear()
        s = s[b2:e2]

    return answer
  • ์—ฌ๋Ÿฌ๋ฒˆ ๋š๋”ฑ๋š๋”ฑ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์ด์   ์‹ ๋ฌผ์ด ๋‚  ์ง€๊ฒฝ ใ…‹ใ…‹ใ…‹
    • ๋  ๊ฑฐ ๊ฐ™์€๋ฐ ์•ˆ๋˜๋‹ˆ๊นŒ
      • ๋” ์งœ์ฆ๋‚จ.
  • ์ข€๋งŒ ๋จธ๋ฆฌ๋ฅผ ์ผ์œผ๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ ธ์„ ๊ฑฐ ๊ฐ™์€๋ฐ...
    • ๊ฒฐ๊ตญ ํฌ๊ธฐํ•˜๊ณ 
  • for๋ฌธ์„ ๋ฌด์‹ํ•˜๊ฒŒ ๋Œ๋ฆฌ๋ฉด์„œ ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ์œผ๋กœ ํ•ด๊ฒฐ
def solution(s):
    answer = 1
    
    if len(set(s)) == 1:
        return len(s)
    
    for i in range(len(s)):
        for j in range(i,len(s)):
            word1 = s[i:j+1]
            word2 = word1[::-1]
            if word1 == word2:
                answer = max(answer, len(word1))

    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (2.43ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (2.80ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.37ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (1.38ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2.66ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (2.76ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (1.46ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (1.27ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (1.34ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.80ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (1.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	์‹คํŒจ (5.24ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (4.65ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (8.61ms, 10.1MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	์‹คํŒจ (1.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (1.47ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	์‹คํŒจ (1.45ms, 10.1MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (2088.40ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (2255.26ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด
    • ๋Œ€๋ถ€๋ถ„ 2์ค‘ for๋ฌธ + ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ์œผ๋กœ ํ’€์—ˆ๊ณ ...
    • DP ํ’€์ด. ๊ณ ๊ธ‰์ ธ ๋ณด์ธ๋‹ค. ใ… .ใ… 
def solution(s):
    dp = [[0]*len(s) for _ in range(len(s))]

    answer = 1
    for i in range(len(dp)):
        dp[i][i] = 1
        answer = 1
    for i in range(len(dp)-1):
        if s[i] == s[i+1]:
            dp[i][i+1] = 1
            answer = 2

    for k in range(3, len(dp)+1):
        for i in range(len(dp)-k +1):
            j = i + k -1
            if dp[i+1][j-1] == 1 and s[i] == s[j]: 
                dp[i][j] = 1
                answer = k
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.58ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.58ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.75ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.73ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.97ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.57ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (2.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (1.96ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (2.09ms, 10.5MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.55ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.67ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.69ms, 10.2MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (538.53ms, 58MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (612.13ms, 58MB)
728x90
๋ฐ˜์‘ํ˜•