Skip to content

[Algorithm] 명예의 전당(1) #34

Description

@hwangJi-dev

💬 Idea

  1. honorArray의 개수가 k보다 작을 때에는 honorArray에 append
  2. honorArray의 개수가 k보다 클 때에는 honorArray를 정렬해서 i가 honorArray의 최솟값보다 크다면 honorArray의 마지막 인덱스를 i로 대체하여 명예의 전당에 올린다.

💬 풀이

func solution(_ k:Int, _ score:[Int])->[Int]{varhonorArray:[Int]=[]varlastArray:[Int]=[]foriin score {if honorArray.count < k {
honorArray.append(i)}else{
honorArray.sort(by:>)if i >= honorArray.last! {
honorArray.removeLast()
honorArray.append(i)}}
honorArray.sort(by:>)
lastArray.append(honorArray.last!)}return lastArray
}

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