Hi, There!
안녕하세요, 바오밥입니다.
목차
- 문제
- 풀이
문제
문제 내용
https://school.programmers.co.kr/learn/courses/30/lessons/120924
풀이
나의 풀이
- 주어지는 값이 등차수열, 등비수열 밖에 없다.
- 따라서, 등차수열인지 아닌지만 확인한 다음 등차수열인 경우에는 등차 값을 더하고, 등비수열인 경우에는 등비 값을 더하도록 풀이했다.
class Solution {
public int solution(int[] common) {
return (common[common.length-1] - common[common.length-2]) == (common[1] - common[0])
? common[common.length-1]+common[common.length-1] - common[common.length-2]
: common[common.length-1]*(common[common.length-1] / common[common.length-2]);
}
}
- 위 풀이가 너무 불친절한 것 같아서 아래와 같이 변환했다.
- 동작은 동일하다.
class Solution {
public int solution(int[] common) {
boolean isAp = (common[common.length-1] - common[common.length-2]) == (common[1] - common[0]);
int value = 0;
if(isAp) value = common[common.length-1] - common[common.length-2];
else value = common[common.length-1] / common[common.length-2];
return isAp
? common[common.length-1]+value
: common[common.length-1]*value;
}
}
'Dev > PS' 카테고리의 다른 글
[백준-완전탐색] 14568 2017 연세대학교 프로그래밍 경시대회 (사탕) (0) | 2023.09.23 |
---|---|
[백준-완전탐색] 1816 암호 키 (0) | 2023.09.23 |
[프로그래머스-코딩테스트 입문] 연속된 수의 합 (0) | 2023.09.21 |
[프로그래머스-코딩테스트 입문] 종이 자르기 (0) | 2023.09.21 |
[프로그래머스-코딩테스트 입문] 문자열 밀기 (0) | 2023.08.30 |