코테 노트/프로그래머스

Level 3 길 찾기 게임 <2019 KAKAO BLIND RECRUITMENT> Python 3

화요밍 2021. 9. 2. 18:11
728x90
반응형

https://programmers.co.kr/learn/courses/30/lessons/42892?language=python3 

 

코딩테스트 연습 - 길 찾기 게임

[[5,3],[11,5],[13,3],[3,5],[6,1],[1,3],[8,6],[7,2],[2,2]] [[7,4,6,9,1,8,5,2,3],[9,6,5,8,1,4,3,2,7]]

programmers.co.kr

 

최종 코드

 

GitHub - hwayeon351/Programmers-Algorithms: 프로그래머스 알고리즘 소스 코드 모음

프로그래머스 알고리즘 소스 코드 모음. Contribute to hwayeon351/Programmers-Algorithms development by creating an account on GitHub.

github.com

import sys
sys.setrecursionlimit(20000000)
class Node:
    def __init__(self, x, y, num):
        self.x = x
        self.y = y
        self.num = num
        self.left = None
        self.right = None
    def __str__(self):
        return str(self.num)

class Tree:
    def __init__(self):
        self.root = None
    def get_root(self):
        return self.root
    def insert(self, x, y, num):
        new_node = Node(x, y, num)
        cur = self.root
        if cur == None:
            self.root = new_node
            return
        while True:
            parent = cur
            if x < cur.x: 
                cur = cur.left
                if not cur:
                    parent.left = new_node
                    return
            else:
                cur = cur.right
                if not cur:
                    parent.right = new_node
                    return
                
    def preorder_traverse(self, cur):
        global preorder
        if not cur:
            return
        preorder.append(cur.num)
        self.preorder_traverse(cur.left)
        self.preorder_traverse(cur.right)
        
    def postorder_traverse(self, cur):
        global postorder
        if not cur:
            return
        self.postorder_traverse(cur.left)
        self.postorder_traverse(cur.right)
        postorder.append(cur.num)
    
def solution(nodeinfo):
    global preorder, postorder
    preorder = []
    postorder = []
    answer = []
    for i in range(1, len(nodeinfo)+1):
        nodeinfo[i-1].append(i)
    nodeinfo.sort(key=lambda x:(-x[1], x[0]))
    tree = Tree()
    tree.insert(nodeinfo[0][0], nodeinfo[0][1], nodeinfo[0][2])
    for i in range(1, len(nodeinfo)):
        tree.insert(nodeinfo[i][0], nodeinfo[i][1], nodeinfo[i][2])
    tree.preorder_traverse(tree.get_root())
    tree.postorder_traverse(tree.get_root())
    answer.append(preorder)
    answer.append(postorder)
    return answer

풀이 과정

풀이 시간 1시간 7분

 

import sys
sys.setrecursionlimit(20000000)
class Node:
    def __init__(self, x, y, num):
        self.x = x
        self.y = y
        self.num = num
        self.left = None
        self.right = None
    def __str__(self):
        return str(self.num)

class Tree:
    def __init__(self):
        self.root = None
    def get_root(self):
        return self.root
    def insert(self, x, y, num):
        new_node = Node(x, y, num)
        cur = self.root
        if cur == None:
            self.root = new_node
            return
        while True:
            parent = cur
            if x < cur.x: 
                cur = cur.left
                if not cur:
                    parent.left = new_node
                    return
            else:
                cur = cur.right
                if not cur:
                    parent.right = new_node
                    return
                
    def preorder_traverse(self, cur):
        global preorder
        if not cur:
            return
        preorder.append(cur.num)
        self.preorder_traverse(cur.left)
        self.preorder_traverse(cur.right)
        
    def postorder_traverse(self, cur):
        global postorder
        if not cur:
            return
        self.postorder_traverse(cur.left)
        self.postorder_traverse(cur.right)
        postorder.append(cur.num)
    
def solution(nodeinfo):
    global preorder, postorder
    preorder = []
    postorder = []
    answer = []
    for i in range(1, len(nodeinfo)+1):
        nodeinfo[i-1].append(i)
    nodeinfo.sort(key=lambda x:(-x[1], x[0]))
    tree = Tree()
    tree.insert(nodeinfo[0][0], nodeinfo[0][1], nodeinfo[0][2])
    for i in range(1, len(nodeinfo)):
        tree.insert(nodeinfo[i][0], nodeinfo[i][1], nodeinfo[i][2])
    tree.preorder_traverse(tree.get_root())
    tree.postorder_traverse(tree.get_root())
    answer.append(preorder)
    answer.append(postorder)
    return answer

참고

 

Level 3 매칭 점수 <2019 KAKAO BLIND RECRUITMENT> Python 3

https://programmers.co.kr/learn/courses/30/lessons/42893?language=python3 코딩테스트 연습 - 매칭 점수 매칭 점수 프렌즈 대학교 조교였던 제이지는 허드렛일만 시키는 네오 학과장님의 마수에서 벗어나, 카..

hwayomingdlog.tistory.com

  • 7. 블록 게임 -> 

 

728x90
반응형