본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 181929. 원소들의 곱과 합 JAVA Kotlin

by Bhinney 2023. 12. 12.
 

프로그래머스

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

programmers.co.kr


❓ 문제

정수가 담긴 리스트 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
   }
}

 

댓글