Skip to content

[Algorithm] 택배상자 #183

Description

@hwangJi-dev

💬 문제

https://school.programmers.co.kr/learn/courses/30/lessons/131704


💬 Idea

  • 택배상자는 1번 상자부터 n번 상자까지 번호가 증가하는 순서대로 컨테이너 벨트에 놓여 전달되므로 따로 컨테이너 벨트에 대한 stack을 만들 필요가 없다. c에 컨테이너 벨트에 놓이는 택배상자의 번호를 저장해준다.
  • order를 돌면서 c가 순서보다 작거나 같다면 보조 컨테이너벨트에 c를 순서대로 보관하고, c를 증가시킨다.
  • 이후 보조 컨테이너벨트의 마지막 원소가 순서와 같다면 싣을 수 있는 상자의 개수를 늘리고, 그렇지 않다면 반복문을 빠져나온다.

💬 풀이

import Foundation
func solution(order:[Int])->Int{varassistant:[Int]=[]varc=1varboxCnt=0foriin order {while c <= i {
assistant.append(c)
c +=1}if assistant.last == i {
assistant.removeLast()
boxCnt +=1}else{break}}return boxCnt
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions