Hard deck/reindexing d2

029 : Binary search 기본

서버관리자 페페 2023. 6. 19. 10:09

 

 

 

 

 

 

 

 

 

 

pointer와 (pointer-value) connector 준비

 

팰린드롬 처럼 값을 다 돌아야 결과가 나올 때는 바로 OEC하지 못하고 surviving boolean 사용

 

m기준

- 오른쪽 +1

- 왼쪽 -1

 

 

 

 

 

 

 

 

 

 

 

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // ISC
        Scanner sc = new Scanner(System.in);
        int V = sc.nextInt();

        // D2F
        int[] TP = new int[V+1];
        for (int i = 1; i <= V; i++) {
            TP[i] = sc.nextInt();
        }

            // checker
        int tC = sc.nextInt();
        int[] checker = new int[tC+1];
        for (int i = 1; i <= tC+1; i++) {
            checker[i] = sc.nextInt();
        }
        Arrays.sort(TP);

        // OP
        for (int i = 1; i <= tC; i++) {

            int target = checker[i];
            int s = 1;
            int e = V;
            boolean checked = false;

            while (s <= e) {
                int m = s + e / 2;
                if (TP[m] > target) {
                    e = m-1;
                } else if (TP[m] < target) {
                    s = m+1;
                } else {
                    checked = true;
                    break;
                }
            }

            if (checked)
                System.out.println(1);
            else
                System.out.println(0);
        }

        // OEC


    }
}

 

'Hard deck > reindexing d2' 카테고리의 다른 글

009 : sliding window  (0) 2023.06.19
008 : 좋은 수 구하기  (0) 2023.06.19
007 : 투 포인터 > 무작위 배열의 2가지 합  (0) 2023.06.19
006 : 투 포인터  (0) 2023.06.19
030 : 블루레이 만들기  (1) 2023.06.19