❓ 문제
정수가 담긴 리스트 num_list가 주어질 때, 모든 원소들의 곱이 모든 원소들의 합의 제곱보다 작으면 1을 크면 0을 return하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤ num_list의 길이 ≤ 10
- 1 ≤ num_list의 원소 ≤ 9
📎 예시
num_list | result |
[3, 4, 5, 2, 1] | 1 |
[5, 7, 8, 3] | 0 |
✍🏻 JAVA
import java.util.stream.IntStream;
class Solution {
public int solution(int[] num_list) {
int multiply = IntStream.of(num_list).reduce((a, b) -> a * b).orElse(0);
int sum = (int) Math.pow(IntStream.of(num_list).sum(), 2);
return multiply < sum ? 1 : 0;
}
}
✍🏻 Kotlin
import kotlin.math.pow
class Solution {
fun solution(num_list: IntArray): Int {
val sum = num_list.sum().toDouble().pow(2).toInt()
val multiply = num_list.reduce { a, b -> a * b}
return if (sum > multiply) 1 else 0
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 81303. 표 편집 JAVA (0) | 2024.01.09 |
---|---|
[프로그래머스] 181897. 리스트 자르기 JAVA Kotlin (1) | 2024.01.03 |
[프로그래머스] 181928. 이어 붙인 수 JAVA Kotlin (0) | 2023.12.12 |
[프로그래머스] 181934. 조건 문자열 JAVA Kotlin (2) | 2023.12.05 |
[프로그래머스] 132266. 부대 복귀 JAVA (1) | 2023.12.05 |
댓글