이 또한 노가다 경우의 수 나누어서 통과했다 자괴감 느껴져..
그나마 메서드 만들어서 해보자! 하고 수정한 코드가 아랜데
근데 메서드가 private 일 필요도 static 일 필요도 없었다.
import java.lang.Math;
import java.util.*;
class Solution {
public int solution(int a, int b, int c, int d) {
int answer = 0;
int[] intArr = new int[]{a,b,c,d};
if (a == b && b == c && c == d) {
return a * 1111;
}
else if (a == b && b == c) {
return threeSame(a, b, c, d);
}
else if (a == b && b == d) {
return threeSame(a, b, d, c);
}
else if (a == c && d == c) {
return threeSame(a, c, d, b);
}
else if (d == b && b == c) {
return threeSame(d, b, c, a);
}
else if (a == b && c == d) {
return eachTwoSame(a, c);
}
else if (a == c && b == d) {
return eachTwoSame(a, b);
}
else if (a == d && b == c) {
return eachTwoSame(a, b);
}
else if (a == b) {
return justTwoSame(c, d);
}
else if (a == c) {
return justTwoSame(b, d);
}
else if (a == d) {
return justTwoSame(b, c);
}
else if (b == c) {
return justTwoSame(a, d);
}
else if (b == d) {
return justTwoSame(a, c);
}
else if (c == d) {
return justTwoSame(a, b);
}
else return Math.min(Math.min(a,b), Math.min(c,d));
}
private static int threeSame(int x, int y, int z, int m) {
return (10 * x + m)*(10 * x + m);
}
private static int eachTwoSame(int x, int y) {
return (x + y) * Math.abs(x - y);
}
private static int justTwoSame(int x, int y) {
return x * y;
}
}
다른 분 풀이 봤는데
sort 를 사용해서 더 깔끔하게 푼 것 보고 감탄했다.
'정렬'해놓으면 배열의 요소 같은지 경우의 수 따지는게 훨씬 수월해지니까!
와 세개 같은 거의 경우의 수도 정렬하고 확실히 같은 3개 수인 dice[1]을 다를 수 있는 수 dice[0]이랑 dice[3] 더한거에서 빼주는 걸로 하니까 저렇게 요약이 되네
정렬했으니 절대값 메서드도 필요 없고 조건문도 간단해지고
맨 위에서부터 같은 수 많은거로 거르고 내려오니까 조건문도 간단해지고..
고민하고 코드를 짜면 이렇게 나오겠구나 한 코드였다 대단하다..
머리 쓰자ㅠ
import java.util.Arrays;
class Solution {
public int solution(int a, int b, int c, int d) {
int[] dice = { a, b, c, d };
Arrays.sort(dice);
int ans = 0;
if (dice[0] == dice[3]) {
ans = 1111 * dice[3];
} else if (dice[0] == dice[2] || dice[1] == dice[3]) {
ans = (int) Math.pow(dice[1] * 10 + (dice[0] + dice[3] - dice[1]), 2);
} else if (dice[0] == dice[1] && dice[2] == dice[3]) {
ans = (dice[0] + dice[3]) * (dice[3] - dice[0]);
} else if (dice[0] == dice[1]) {
ans = dice[2] * dice[3];
} else if (dice[1] == dice[2]) {
ans = dice[0] * dice[3];
} else if (dice[2] == dice[3]) {
ans = dice[0] * dice[1];
} else {
ans = dice[0];
}
return ans;
}
}
'코테 공부 > java' 카테고리의 다른 글
StringBuilder (0) | 2023.05.15 |
---|---|
list이름.toArray(배열 객체) (0) | 2023.05.14 |
boolean 변수 초기화? (0) | 2023.05.06 |
Integer.toBinaryString(i)-이진문자열, stream.filter(람다식)-이렇게도 활용. 범위 내 0,5로만 이루어진 모든 정수 배열 문제. (0) | 2023.05.06 |
ArrayList<Integer> 를 int[] 배열로 변환 (0) | 2023.05.05 |