파비의 매일매일 공부기록

2023.07.30 Today's Challenge 본문

Problem Solving/LeetCode

2023.07.30 Today's Challenge

fabichoi 2023. 7. 30. 23:45

https://leetcode.com/problems/strange-printer/

 

Strange Printer - LeetCode

Can you solve this real interview question? Strange Printer - There is a strange printer with the following two special properties: * The printer can only print a sequence of the same character each time. * At each turn, the printer can print new character

leetcode.com

오랫만에 릿코드 고고싱

class Solution:
    def strangePrinter(self, s: str) -> int:
        n = len(s)
        dp = [[n] * n for _ in range(n)]
        for length in range(1, n+1):
            for left in range(n-length+1):
                right = left + length - 1
                j = -1
                for i in range(left, right):
                    if s[i] != s[right] and j == -1:
                        j = i
                    if j != -1:
                        dp[left][right] = min(dp[left][right], 1+dp[j][i]+dp[i+1][right])
                
                if j== -1:
                    dp[left][right] = 0
        return dp[0][n-1] + 1
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.08.01 Today's Challenge  (0) 2023.08.01
2023.07.31 Today's Challenge  (0) 2023.07.31
2023.07.29 Today's Challenge  (0) 2023.07.29
2023.07.28 Today's Challenge  (0) 2023.07.28
2023.07.27 Today's Challenge  (0) 2023.07.27
Comments