본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / n보다 커질 때까지 더하기 / Java

by unknownomad 2024. 1. 10.

문제

 

풀이

class Solution {
    public int solution(int[] numbers, int n) {
        int answer = 0;
        for(int i = 0; i < numbers.length; i++) {
            if(answer <= n) {
                answer += numbers[i];
            }
        }
        return answer;
    }
}
import java.util.Arrays;

class Solution {
    public int solution(int[] numbers, int n) {     
        return Arrays.stream(numbers)
            .reduce(0, (answer, i) -> n >= answer ? answer + i : answer);
    }
}

 

 

출처

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

댓글