programmers- lv.1 (크레인 인형뽑기 게임)

이예송·2023년 7월 15일

PS

목록 보기
38/97

문제링크: 크레인 인형뽑기 게임

✍🏻 Information

content
언어python
난이도⭐️⭐️
풀이시간40분
제출횟수3
인터넷검색유무yes




🍒 My Code

def solution(board, moves):
    answer = 0
    item = []
    size = len(board)
    point = [(size-1) for i in range(size)]
    #각 줄마다 제일 위에 있는 인형의 위치를 파악해서 point list에
    for x in range(size):
        for y in range(size):
            if board[y][x]!=0:
                point[x]=y
                break
    #움직여가며 1)point위치를 바꾸고 2)인형을 item에 넣음(같은 인형이면 터침)
    for move in moves:
        if point[move-1]<size:
            if len(item)>0 and item[-1]==board[point[move-1]][move-1]:
                answer+=2
                item.pop()
            else:
                item.append(board[point[move-1]][move-1])
            point[move-1]+=1
        
    return answer




💡 What I learned

  • 검색한 내용: list에서 원소를 뽑고 싶었는데 .pop()이 기억이 안났다
  • range out of index 오류에도 꽤 시달렸는데 point를 움직이기 전에 비교를 해야됐는데 그 이후에 해서 발생했던것,,
  • 문제 잘못이해해서 처음에는 item에 인형을 넣지 않고 바로 앞 인형과 같은지만 검색해서 같으면 +2해주었는데 그런것이 아니었다. 바로 앞 인형과만 비교하면 안됐다.
  • 좋은 풀이
def solution(board, moves):
    stacklist = []
    answer = 0

    for i in moves:
        for j in range(len(board)):
            if board[j][i-1] != 0:
                stacklist.append(board[j][i-1])
                board[j][i-1] = 0

                if len(stacklist) > 1:
                    if stacklist[-1] == stacklist[-2]:
                        stacklist.pop(-1)
                        stacklist.pop(-1)
                        answer += 2     
                break

    return answer

-> 확인해가면서 넣고 빼고를 해도 됐다

0개의 댓글