Skip to content

[Algorithm] 푸드 파이트 대회 #66

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 푸드 파이트 대회](https://school.programmers.co.kr/learn/courses/30/lessons/134240)


💬 Idea

[1(물), 3, 4, 6] 을 예로 들어본다면

1번 인덱스(1번 음식)은 → 두명이서 1개씩 나누어먹을 수 있다.

2번 인덱스(2번 음식)은 → 두명이서 2개씩 나누어먹을 수 있다.

3번 인덱스(3번 음식)은 → 두명이서 3개씩 나누어먹을 수 있다.

  • 따라서 1부터 food의 count - 1까지 돌며 해당 수를 food[$0] / 2 만큼 반복하는 Array를 flat화한 뒤 str을 도출한다.
  • 이렇게 도출된 str은 122333 이다.
  • 마지막으로 중간에 물을 넣어주고, 대회의 공정성을 위해 음식을 먹는 순서를 맞춰주기 위해 str을 reversed하여 결과를 도출한다.

💬 풀이

func solution(food:[Int])->String{letstr=(1...food.count -1).flatMap{Array(repeating: $0, count:food[$0]/2)}.map{String($0)}.joined()return str +"0"+ str.reversed()}

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