inblog logo
|
jay0628
    programmers Java

    [Java] 22. 등차수열의 특정한 항만 더하기

    김주희's avatar
    김주희
    Jun 10, 2025
    [Java] 22. 등차수열의 특정한 항만 더하기
    Contents
    1. 문제 설명, 제한 사항, 입출력 예시2. 아이디어3. 풀이 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181931

    1. 문제 설명, 제한 사항, 입출력 예시

    notion image
    notion image
    notion image
     

    2. 아이디어

    💡
    1. 배열의 크기 = 배열.length
    1. 각 항의 공통 패턴 = a + d * i
     

    3. 풀이 코드

    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; } }
    GPT
    import java.util.stream.IntStream; class Solution { public int solution(int a, int d, boolean[] included) { return IntStream.range(0, included.length) .filter(i -> included[i]) .map(i -> a + d * i) .sum(); } }
    Share article

    jay0628

    RSS·Powered by Inblog