커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
시간 초과가 나는데 어떤 부분을 조정해야할지 감이 오지 않습니다.
[스킬업] 실무에 바로 쓰이는 알고리즘 by Python v0
2주차
북마크
박*수
댓글
1
추천
0
조회수
11
조회수
11
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


큐 2 (실버4) - https://www.acmicpc.net/problem/18258

문제를 풀고 있습니다.


시간 초과가 나는데 어떤 부분을 조정해야할지 감이 오지 않습니다.



스파르타 즉문즉답





작성한 코드 및 에러 메세지

class Node:
    def __init__(self, val):
        self.val = val
        self.next = None


class LinkedList:
    def __init__(self):
        self.head = None
        self.count = 0


    def push(self, val):
        self.count += 1
        if not self.head:
            self.head = Node(val)
            return
        
        node = self.head
        new_node = Node(val)
        while node.next:
            node = node.next
        node.next = new_node
    
    def pop(self):
        if self.count == 0:
            print(-1)
            return
        node = self.head
        self.head = node.next
        self.count -= 1
        print(node.val)


    def size(self):
        print(self.count)


    def empty(self):
        if self.count == 0:
            print(1)
        else:
            print(0)
    
    def front(self):
        if self.count == 0:
            print(-1)
            return
        print(self.head.val)
    
    def back(self):
        if self.count == 0:
            print(-1)
            return
        node = self.head
        while node.next:
            node = node.next
        print(node.val)


N = int(input())


linked_list = LinkedList()


for _ in range(N):
    input_string = input()


    if 'push' in input_string:
        command, value = input_string.split()
        value = int(value)
        linked_list.push(value)
    else:
        # pop, size, empty, front, back
        command = input_string
        if command == 'pop':
            linked_list.pop()
        elif command == 'size':
            linked_list.size()
        elif command == 'empty':
            linked_list.empty()
        elif command == 'front':
            linked_list.front()
        elif command == 'back':
            linked_list.back()

       



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴