CodeSignal 12번 sortByHeight 문제풀이

2020. 1. 20. 22:56·알고리즘/CodeSignal

이 문제는 입력 수열에 -1으로 표시되는 나무들은 그 자리에 두고 나머지 사람들만

오름차순으로 정렬해 반환하는 문제입니다.

def sortByHeight(a):
    treePos,copy=[],a[:]
    for i in range(copy.count(-1)):
        treePos.append(copy.index(-1)+i)
        del copy[copy.index(-1)]
    copy.sort()
    [copy.insert(i,-1) for i in treePos]
    return copy

전 나무들의 위치를 저장하면서 모두 빼준다음

사람들만 있는 수열을 정렬하고, 정렬한 수열에 나무들을 원래 위치에 넣어주었습니다.

이 코드는 for문에서 enumerate를 이용해 코드 길이를 줄일수 있습니다.

심심하시다면 한번 시도해보세요

'알고리즘 > CodeSignal' 카테고리의 다른 글

CodeSignal 14번 alternatingSums 문제풀이  (0) 2020.01.21
CodeSignal 13번 reverseInParentheses 문제풀이  (0) 2020.01.21
CodeSignal 11번 allLongestStrings 문제풀이  (0) 2020.01.20
CodeSignal 10번 commonCharacterCount 문제풀이  (0) 2020.01.20
CodeSignal 9번 allLongestStrings 문제풀이  (0) 2020.01.20
'알고리즘/CodeSignal' 카테고리의 다른 글
  • CodeSignal 14번 alternatingSums 문제풀이
  • CodeSignal 13번 reverseInParentheses 문제풀이
  • CodeSignal 11번 allLongestStrings 문제풀이
  • CodeSignal 10번 commonCharacterCount 문제풀이
​​​​
​​​​
  • ​​​​
    개발 블로그
    ​​​​
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 오늘 배운것들
      • 프로젝트
        • NodeJs-유튜브 음질 뷰어
        • 직접 설계해본 ERD
        • URL 단축&방문수 분석 사이트
        • GPT로 영어 공부하기
      • 알고리즘
        • 백준
        • CodeSignal
        • 프로그래머스
      • JavaScript
        • Npm Modules
        • VanillaJS
        • NodeJS
      • CI&CD
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
​​​​
CodeSignal 12번 sortByHeight 문제풀이
상단으로

티스토리툴바