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] 과일 장수 #33

Closed
hwangJi-dev opened this issue Dec 6, 2022 · 0 comments
Closed

[Algorithm] 과일 장수 #33

hwangJi-dev opened this issue Dec 6, 2022 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Dec 6, 2022

💬 문제

[코딩테스트 연습 - 과일 장수](https://school.programmers.co.kr/learn/courses/30/lessons/135808)

💬 Idea

  1. score 내림차순 정렬
  2. for-stride로 배열 순회 (m 개수만큼)
  3. 해당 값 * m 을 sum에 더하기

💬 풀이

func solution(_ k:Int, _ m:Int, _ score:[Int]) -> Int {
    
    // 1. score 내림차순 정렬
    var score = score.sorted(by: >)
    var sum = 0
    
    // 2. stride로 배열 순회 (m 개수만큼)
    // 3. 해당 값 * m 을 sum에 더하기
    if score.count != m {
        for i in stride(from: m - 1, to: score.count, by: m) {
            sum += score[i] * m
        }
    } else {
        sum += score[score.count - 1] * m
    }
    
    return sum
}
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