Python/Coding_Problems

프로그래머스(Programmers)_Level1_같은숫자는싫어

yunajoe 2022. 12. 10. 11:02

# 처음에 생각한것 

s = list("baabaa")

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    wanted_remove_index = [i, i+1]
    while len(wanted_remove_index):  
      s = [v for idx_s, v in enumerate(s) if idx_s not in wanted_remove_index]
      wanted_remove_index.clear()
       


위에처럼 하면은.. 

wanted_remove_ndex = [1,2] 라고 할 때 
i는 1인 상태에서 위의 인덱스를 뽑아낸것.. 
무튼.. 저걸 다 처리하고나면은 1,2의 인덱스가 없어진 [b,b,a,a] 가 되는데! 
for문의 i는 1에서 2로 넘어간다. 
따랏 [b,b,a,a]에서 a부터 시작되기 때문에 위의 코드는 잘못......
stack 으로 구현

def solution(arr):
    stack = [] 
    for num in arr:
        if len(stack) > 0 and stack[-1] == num:
            continue         
        stack.append(num)      
    return stack
    
    
    

정확성: 71.9
효율성: 28.1
합계: 100.0 / 100.0