본문 바로가기
python

[code signal / 코드시그널] Sort by height

by 래끼 2022. 5. 30.
728x90
반응형

 

 

✨ Description

Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!

 

✨ 문제

몇몇 사람들이 공원에서 일렬로 서 있다. 그들 사이에는 움직일 수 없는 나무들이 있다. 당신의 임무는 나무를 움직이지 않고 사람들을 그들의 키에 따라 오름차순으로 정렬시키는 것이다. 사람들의 키가 매우 클 수 있다.

👉 배열에 -1로 주어진 나무의 위치는 변경시키지 않고 사람의 키에 따라 오름차순으로 정렬시키는 문제입니다!

 

✨ Example

For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be
solution(a) = [-1, 150, 160, 170, -1, -1, 180, 190].

 

✨ My submission

from collections import deque

def solution(a):
    
    result_deque=deque()
    
    tree_location_list = [i for i, value in enumerate(a) if value == -1]
    
    a.sort()
    a_deque=deque(a[len(tree_location_list):])
    
    for i in range(len(a)):
        if i in tree_location_list:
            result_deque.append(-1)
        else:
            result_deque.append(a_deque.popleft())
            
    return list(result_deque)

 

 

 

 

728x90
반응형

댓글