본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 등차수열의 특정한 항만 더하기 / Java

by unknownomad 2023. 10. 18.

문제

 

풀이

class Solution {
    public int solution(int a, int d, boolean[] included) {
        int answer = 0;
        for(int i = 0; i < included.length; i++) {
            if(included[i]) {
                answer += a + (d * i);
            }
        }
        return answer;
    }
}
import java.util.stream.IntStream;

class Solution {
    public int solution(int a, int d, boolean[] included) {
        return IntStream.range(0, included.length)
            .map(idx -> included[idx] ? a + (idx * d) : 0)
            .sum();
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/181931

댓글