Algorithm/Programmers

[Programmers] Lv.0 / 다음에 올 숫자 / Java

unknownomad 2024. 3. 19. 07:48

문제

 

풀이

class Solution {
    public int solution(int[] common) {
        int n = common.length;        
        int commonDifferenceOrRatio = common[1] - common[0];
        
        if (common[n - 1] - common[n - 2] == commonDifferenceOrRatio) {
            return common[n - 1] + commonDifferenceOrRatio;
        } else {
            int ratio = common[n - 1] / common[n - 2];
            return common[n - 1] * ratio;
        }
    }
}
class Solution {
    public int solution(int[] common) {
        int answer = 0;
        int x = common[1] - common[0];
        int y = common[2] - common[1];

        if (x == y) {
            answer = common[common.length - 1] + y;
        } else {
            answer = common[common.length - 1] * common[2] / common[1];
        }
        return answer;
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/120924