Skip to content

Latest commit

 

History

History
126 lines (95 loc) · 3.57 KB

Wrong_Answer.md

File metadata and controls

126 lines (95 loc) · 3.57 KB

틀렸습니다 : 히든 테스트 케이스에 걸린 경우

정수 오버플로우


시작 인덱스 설정을 잘못 한 경우

static boolean test(String input, int start, int end){
        // end is exclusive

        if(end - start <= 1)
            return true;

        int mid = (start + end) / 2;

        // (x)
        for (int i = 0; i < mid; i++) {
            // ...
        }

        // (o)
        for (int i = start; i < mid; i++) {
            // ...
        }

바뀐 변수 값 재사용

1. 반복문 내부에서 반복자 값을 수정한 경우

// Before
class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2];
        
        for(int row = 1; row <= Math.sqrt(yellow); row++){
            if(yellow % row != 0)
                continue;
            
            int col = yellow / row;
            
            if(2 * ( (row += 2) + (col += 2) - 2) == brown){
                // 여기서 row 자체를 2씩 증가시켜 버리기 때문에 반복문이 1이 아니라 3 간격으로 건너뛰게 된다.
                // 딴에 연산 중복을 줄인다고 한 거지만, 중복이 일어나는 건 어차피 정답을 찾았을 때 단 한번 뿐이므로 큰 의미가 없다.
                answer[0] = col;
                answer[1] = row;
                break;
            }
        }
        
        return answer;
    }
}
// After
class Solution {
    public int[] solution(int brown, int yellow) {
        int[] answer = new int[2];
        
        for(int row = 1; row <= Math.sqrt(yellow); row++){
            if(yellow % row != 0)
                continue;
            
            int col = yellow / row;
            
            if(2 * ( (row + 2) + (col + 2) - 2) == brown){
                answer[0] = col + 2;
                answer[1] = row + 2;
                break;
            }
        }
        
        return answer;
    }
}

2.행렬 곱셈

long na = multiplyRowAndColum(a, b, a, c);
        long nb = multiplyRowAndColum(a, b, b, d);
        long nc = multiplyRowAndColum(c, d, a, c);
        long nd = multiplyRowAndColum(c, d, b, d);

        if(N.mod(TWO).equals(BigInteger.ONE)){
            na = multiplyRowAndColum(na, nb, 1, 1);
            nb = multiplyRowAndColum(na, nb, 1, 0); // 직전 라인에서 바뀐 na값을 재사용하고 있다.
            nc = multiplyRowAndColum(nc, nd, 1, 1);
            nd = multiplyRowAndColum(nc, nd, 1, 0); // 여기도 마찬가지
        }

        return new long[][]{
            {na, nb},
            {nc, nd}
        };

경곗값


N이 0으로 주어졌을 때

백준 18110 solved.ac


기타

백준 1009 분산처리
백준 16976 배열 복원하기
테스트 케이스 배열의 크기를 늘렸을 때 반례를 발견할 수 있었다.