https://school.programmers.co.kr/learn/courses/30/lessons/120862
import java.util.*;
class Solution {
public int solution(int[] numbers) {
//음수 두 개, 양수 두 개 곱한게 최대
ArrayList<Integer> negative = new ArrayList<>(); //음수
ArrayList<Integer> positive = new ArrayList<>(); //양수
for(int i=0; i<numbers.length; i++){
if(numbers[i] < 0){
negative.add(numbers[i]);
}else if(numbers[i] > 0){
positive.add(numbers[i]);
}
}
int biggestNegative = 0;
int biggestPositive = 0;
if(negative.size() == 1 && positive.size() == 1){
return negative.get(0) * positive.get(0);
}else if(negative.size() > 1){
Collections.sort(negative, Collections.reverseOrder());
biggestNegative = negative.get(0) * negative.get(1);
}else if(positive.size() > 1){
Collections.sort(positive, Collections.reverseOrder());
biggestPositive = positive.get(0) * positive.get(1);
}
if(biggestNegative < biggestPositive){
return biggestPositive;
}else{
return biggestNegative;
}
}
}
else if문을 써버리면 if문의 조건이 충족했을 때 다음 if문으로 넘어가지 않는다. 그래서 else if문이 아닌, if문을 3개를 써 주어야 한다.
import java.util.*;
class Solution {
public int solution(int[] numbers) {
//음수 두 개, 양수 두 개 곱한게 최대
ArrayList<Integer> negative = new ArrayList<>(); //음수
ArrayList<Integer> positive = new ArrayList<>(); //양수
Arrays.sort(numbers);
for(int i=0; i<numbers.length; i++){
if(numbers[i] < 0){
negative.add(numbers[i]);
}else if(numbers[i] > 0){
positive.add(numbers[i]);
}
}
int biggestNegative = 0;
int biggestPositive = 0;
if(numbers.length == 2){
return numbers[0] * numbers[1];
}
if(negative.size() >= 2){
Collections.sort(negative, Collections.reverseOrder());
biggestNegative = negative.get(0) * negative.get(1);
}
if(positive.size() >= 2){
Collections.sort(positive, Collections.reverseOrder());
biggestPositive = positive.get(0) * positive.get(1);
}
if(biggestNegative < biggestPositive){
return biggestPositive;
}else{
return biggestNegative;
}
}
}