Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hwangJi-dev opened this issue Jan 10, 2023 · 0 comments
Closed

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

hwangJi-dev opened this issue Jan 10, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 10, 2023

💬 문제

[코딩테스트 연습 - 푸드 파이트 대회](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 {
    let str = (1...food.count - 1).flatMap{ Array(repeating: $0, count: food[$0] / 2) }.map{ String($0) }.joined()
    return str + "0" + str.reversed()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant