Post

leetcode(리트코드)-838 Push Dominoes(PYTHON)

leetcode 838 - Push Dominoes 문제입니다.

1. 문제

https://leetcode.com/problems/push-dominoes/


2. Input , Output


3. 분류 및 난이도

Medium 난이도 문제입니다.


4. 문제 해석

  • dominoes가 들어온다.
  • dominoes의 R은 오른쪽으로 도미노이고 L은 왼쪽으로 도미노한다..
  • 예제처럼 2개가 쌓여있다해서 힘이 더 쌔고 그런건 없는 것같다.

5. code

코드설명

  • ’.’을 기준으로 왼쪽와 오른쪽을 탐색해서 “LL,LR,RL,RR”인 경우마다 처리를 다르게 해주어 해결했다.

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
    def pushDominoes(self, dominoes: str) -> str:
        idx = 0
        dominoes = list(dominoes)
        while idx < len(dominoes) :
            if dominoes[idx] == '.' :
                left = idx
                right = idx
                while left > 0 and dominoes[left] == '.' : 
                    left-=1
                while right+1 < len(dominoes) and dominoes[right] == '.' : 
                    right+=1
                if dominoes[right] == 'L' :
                    if dominoes[left] == 'R':
                        while left+1 < right-1 : 
                            dominoes[left+1] = 'R'
                            dominoes[right-1] = 'L'
                            left+=1
                            right-=1
                    else : 
                        while left < right :  
                            dominoes[left] = 'L'
                            left+=1
                else : 
                    if dominoes[left] == 'R' :
                        while left <= right : 
                            dominoes[left] = 'R'
                            left+=1 
            idx+=1
        
        return ''.join(dominoes)
                               

6. 결과 및 후기, 개선점

필요시 c++로 짜드립니다.

설명이 필요하다면 댓글을 달아주세요.

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