[Programmers] 등차수열의 특정한 항만 더하기

2024. 8. 11. 18:39
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내 풀이

import Foundation

func solution(_ a:Int, _ d:Int, _ included:[Bool]) -> Int {
    var result = 0
    
    if(included[0] == true){
        result += a
    }
        
    for i in 1..<included.count{
        if(included[i] == true){
            result += (d * i) + a
        }
    }
    
    return result
}

 

 

다른 사람의 풀이

import Foundation

func solution(_ a: Int, _ d: Int, _ included: [Bool]) -> Int {
    return included.indices.filter { included[$0] }.map { a + d * $0 }.reduce(0, +)
}
728x90

'Test > Coding Tests' 카테고리의 다른 글

[Programmers] 이어 붙인 수  (0) 2024.08.11
[BaekJoon] 15552 빠른 A+B  (0) 2024.08.11
[Programmers] 연속된 수의 합  (2) 2023.11.30
[Programmers] 이진수 더하기  (0) 2023.11.28
[Programmers] 치킨 쿠폰  (1) 2023.11.28

BELATED ARTICLES

more