

* 문제 풀이
import java.util.Arrays;
class Solution {
public int solution(int[] sides) {
int answer = 0;
Arrays.sort(sides);
if (sides[2] < sides[0] + sides[1]) {
answer = 1;
} else {
answer = 2;
}
return answer;
}
}
sort() 함수를 사용해 배열을 오름차순으로 정렬한다.
정렬하면 sides[2]가 가장 긴 길이의 변이 된다.
if 조건문을 사용해 조건 확인
* 프로그래머스 다른 코드
class Solution {
public int solution(int[] sides) {
int answer = 2;
int index=0;
int max=0;
for(int i=0; i<sides.length; i++){
if(sides[i]>max) {
max = sides[i];
index = i;
}
}
int sum = 0;
for(int i=0; i<sides.length; i++){
if(i!=index) sum+=sides[i];
}
if(sum>max) answer = 1;
return answer;
}
}
'코테 > 프로그래머스 JAVA Lv.0' 카테고리의 다른 글
| [프로그래머스/java/Lv.0] 홀짝 구분하기 (0) | 2024.12.30 |
|---|---|
| [프로그래머스/java/Lv.0] 배열에서 문자열 대소문자 변환하기 (1) | 2024.12.27 |
| [프로그래머스/java/Lv.0] 대문자로 바꾸기 (0) | 2024.12.19 |
| [프로그래머스/java/Lv.0] A 강조하기 (0) | 2024.12.16 |
| [프로그래머스/java/Lv.0] 특정한 문자를 대문자로 바꾸기 (0) | 2024.12.12 |