[코드트리 챌린지] 코드트리 챌린지 1주차 정리

헨리·2023년 9월 11일
0

코드트리

목록 보기
1/7

9/6 수


어제 본 실력진단결과
4문제인가 풀고 563으로 시작!

일주일 뒤에는 몇점이 될지 기대된다(퇴보만 안하면 다행)




9/11 월


코드트리/구간단위로 완전탐색/아름다운 수열2

https://www.codetree.ai/missions/5/problems/beautiful-sequence-2?&utm_source=clipboard&utm_medium=text

거지같은 내 코드 ..
주소값을 가지고있는 참조자료형인 배열을 복사할때는

int[] temp = brr;

이런식으로 넣으면 배열이 가진 주소값을 넣기때문에
temp가 결국 brr 자체가 되어버리니까

.clone()으로 복사하든
for문으로 하나하나 할당하든
다른 복사방법을 알아야겠다고 느꼈다.

import java.util.*;

public class Main {

    public static int n, m, answer, arr[], brr[];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        arr = new int[n];
        brr = new int[m];
        for(int i=0;i<n;i++){
            arr[i] = sc.nextInt();
        }

        for(int i=0;i<m;i++){
            brr[i] = sc.nextInt();
        }
        for(int i=0;i<n;i++){
            if(check(i)){
                answer++;
            }
        }
        System.out.println(answer);
    }

    public static boolean check(int x){
        int[] temp = brr.clone();
        for(int i=0;i<m;i++){
            if (!isRange(x+i)){
                return false;
            } else {
                for(int j =0;j<m;j++){
                    if(temp[j]==arr[x+i]){
                        temp[j] = 0;
                        break;
                    }
                }
            }
        }
        int sum=0;
        for(int i=0;i<m;i++){
            sum += temp[i];
        }
        if(sum >0){return false;}
        return true;
    }

    public static boolean isRange(int x){
        return 0 <= x && x < n;
    }
}

블로그 정리 1주차..

싸피 과정하면서 챌린지중인데 시간내기가 어렵다(핑계)
수요일에 다시 할 실력진단에선 점수가 올랐으면 좋겠다 ㅜ

profile
개발자?

0개의 댓글