Algorithm

๋ฐฑ์ค€ 10816 | ์ˆซ์ž ์นด๋“œ 2

osean 2020. 11. 7. 01:19

์ˆซ์ž ์นด๋“œ 2 - ๋ฐฑ์ค€ 10816

  • ๋‚˜๋ฅผ ๋ฏธ์น˜๊ฒŒ ๋งŒ๋“  ๋ฌธ์ œ
  • ์ˆ˜๋งŽ์€ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ, ์‹œ๊ฐ„ ์ดˆ๊ณผ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋ฅผ ๊ฒช์œผ๋ฉด์„œ ๋ฏธ์ณ๋ฒ„๋ฆฌ๋Š” ์ค„ ์•Œ์•˜๋‹ค.

์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํ’€์–ด๋ณด๊ธฐ

package ์‹ฌ์„ฑํ—Œ.์•Œ๊ณ ๋ฆฌ์ฆ˜_5์ฃผ์ฐจ.๋ฐฑ์ค€;

import java.io.*;
import java.util.*;

public class ์ˆซ์ž์นด๋“œ2_10816_binary {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer sb = new StringBuffer();
    static int n, m, count;
    static int[] arr1, arr2;
    static boolean[] check;

    public static void main(String[] args) throws Exception {
        n = Integer.parseInt(br.readLine());
        arr1 = new int[n];
        check = new boolean[n];

        StringTokenizer st = new StringTokenizer(br.readLine());
        int cnt = 0;
        while (st.hasMoreTokens()) {
            arr1[cnt] = Integer.parseInt(st.nextToken());
            cnt++;
        }

        Arrays.sort(arr1);

        m = Integer.parseInt(br.readLine());
        arr2 = new int[m];

        st = new StringTokenizer(br.readLine());
        cnt = 0;
        while (st.hasMoreTokens()) {
            arr2[cnt] = Integer.parseInt(st.nextToken());
            count = 0;
            card(arr1, 0, n - 1, arr2[cnt]);
            sb.append(count + " ");
            cnt++;
        }
        bw.write(sb.toString());
        bw.flush();
        bw.close();
    }

    public static void card(int[] arr, int start, int end, int target) {
        if (start > end || start < 0 || end >= n)
            return;
        int mid = (start + end) / 2;
        int mid1 = mid + 1 >= n ? n - 1 : mid + 1;
        int mid2 = mid - 1 < 0 ? 0 : mid - 1;
        if (arr[mid] == target) {
            if (!check[mid]) {
                count++;
                check[mid] = true;
                if (arr[mid2] != target || arr[mid1] == target) {
                    card(arr, mid1, end, target);
                } else if (arr[mid2] == target || arr[mid1] != target) {
                    card(arr, start, mid2, target);
                }
            }
        } else if (arr[mid] > target) {
            card(arr, start, mid2, target);
        } else {
            card(arr, mid1, end, target);
        }
    }
}

์œ„์˜ ๋ถ€ํ’ˆ ์ฐพ๊ธฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹ต๋ฅ ์ด 30% ๋Œ€์ด๊ธด ํ•˜์ง€๋งŒ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์–ด๋Š์ •๋„ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.
๊ทธ๋ž˜์„œ ์ด์ง„ ํƒ์ƒ‰์„ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๊ณ , ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋˜ ๋กœ์ง์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • arr1 ๋ฐฐ์—ด์„ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ target๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•˜์„ ๊ฒฝ์šฐ
    1. boolean ๋ฐฐ์—ด์ธ check ์˜ ํ•ด๋‹น ์ธ๋ฑ์Šค ๊ฐ’์„ true ๋กœ ๋ณ€๊ฒฝํ•ด ์žฌ๋ฐฉ๋ฌธ์‹œ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋Š” ์ค‘๋ณต ์นด์šดํŠธ์˜ ์œ„ํ—˜์„ฑ์„ ์ œ๊ฑฐํ•œ๋‹ค.
    2. cnt++ ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
    3. ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ „, ํ›„ ์ธ๋ฑ์Šค ๋ฐ์ดํ„ฐ์™€ target ์ด ๊ฐ™์„ ๊ฒฝ์šฐ
      • ์ „์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ™์„ ๊ฒฝ์šฐ → end = mid - 1
      • ํ›„์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ™์„ ๊ฒฝ์šฐ → start = mid + 1
  • target ์ด arr1[mid]๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ
    1. end = mid - 1
  • target์ด arr1[mid]๋ณด๋‹ค ํด ๊ฒฝ์šฐ
    1. start = mid + 1

์ด๋Ÿฌํ•œ ๋กœ์ง์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๊ณ , ์ฃผ์–ด์ง„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ๋Š” ์ด์ƒ์—†์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋ฐฑ์ค€์—์„œ ์ฑ„์ ํ–ˆ์„ ๋•Œ ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ˜น์€ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๋‹น์‹œ์—๋Š” ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์—†์—ˆ๊ณ , ๊ทธ๋ƒฅ ๋‚ด ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋‹ค๊ณ ๋งŒ ์ƒ๊ฐํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•Œ์•„๋ณธ ๊ฒฐ๊ณผ, Lower / Upper Bound ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‘์šฉํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊ฑฐ๋ž€ ๋‹ต๋ณ€์„ ๋ณด๊ณ  ์ด๋ฅผ ์ ์šฉํ•ด๋ดค๋‹ค.

Lower / Upper Bound๋ฅผ ์ด์šฉํ•ด ํ’€์–ด๋ณด๊ธฐ

import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer sb = new StringBuffer();
    static int n, m;
    static int[] arr1 = new int[20000001];

    public static void main(String[] args) throws Exception {
        n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        int cnt = 0;
        while (st.hasMoreTokens()) {
            arr1[cnt] = Integer.parseInt(st.nextToken());
            cnt++;
        }

        Arrays.sort(arr1);

        m = Integer.parseInt(br.readLine());

        st = new StringTokenizer(br.readLine());
        cnt = 0;
        while (st.hasMoreTokens()) {
            int result = check(Integer.parseInt(st.nextToken()));
            sb.append(result + " ");
        }
        bw.write(sb.toString());
        bw.flush();
        bw.close();
    }

    public static int check(int target) {
        int lower = lowerbound(arr1, target);
        int upper = upperbound(arr1, target, lower); 

        return upper - lower;
    }

    public static int lowerbound(int[] arr, int target) {
        int lower = 0;
        int upper = arr1.length;
        while (lower < upper) {
            int mid = lower + (upper - lower) / 2;
            if (target <= arr1[mid]) {
                upper = mid;
            } else {
                lower = mid + 1;
            }
        }
        return lower;
    }

    public static int upperbound(int[] arr, int target, int lowerIdx) {
        int lower = lowerIdx;
        int upper = arr1.length;
        while (lower < upper) {
            int mid = lower + (upper - lower) / 2;
            if (target >= arr[mid]) {
                lower = mid + 1;
            } else {
                upper = mid;
            }
        }
        return lower;
    }
}

(์–ด๋–ป๊ฒŒ ๋กœ์ง์ด ์ง„ํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋ ค๊ณ  System.out.println() ์ด ๋งŽ๋‹ค..^^; ๋””๋ฒ„ํ‚น๋„ ํ•  ์ค„ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ..)

์ฒ˜์Œ์—๋Š” check ๋ผ๋Š” ๋ฉ”์†Œ๋“œ์—†์ด main ๋ฉ”์†Œ๋“œ์—์„œ arr2 ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ lower, upper ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋ฐ˜ํ™˜๋˜๋Š” int ๊ฐ’์„ ๊ณ„์‚ฐํ•œ ๊ฒƒ์„ ์ถœ๋ ฅํ–ˆ๋‹ค. ๊ทผ๋ฐ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์ด ๋•Œ๋„ ๋„๋Œ€์ฒด ์™œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ(?) ์ฐธ๊ณ ํ•ด์„œ ์•Œ์•„๋‚ธ ๊ฒฐ๊ณผ, ๋‚ด ์ฝ”๋“œ์—์„œ ์ถœ๋ ฅ์„ ์œ„ํ•œ lower, upper ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณผ์ •์—์„œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด check ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , ํ˜น์‹œ๋‚˜ ํ•˜๋Š” ๋งˆ์Œ์— upper ๋ฉ”์†Œ๋“œ์—๋Š” lower ๋ฉ”์†Œ๋“œ์—์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์„ ๋„ฃ์–ด์ค˜ ์ตœ๋Œ€ํ•œ ์ ์€ ํšŸ์ˆ˜๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer sb = new StringBuffer();
    static int n, m;
    static int[] arr1 = new int[20000001];

    public static void main(String[] args) throws Exception {
        n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        int cnt = 0;
        while (st.hasMoreTokens()) {
            arr1[cnt] = Integer.parseInt(st.nextToken());
            cnt++;
        }

        Arrays.sort(arr1);

        m = Integer.parseInt(br.readLine());

        st = new StringTokenizer(br.readLine());
        cnt = 0;
        while (st.hasMoreTokens()) {
            int result = check(Integer.parseInt(st.nextToken()));
            sb.append(result + " ");
        }
        bw.write(sb.toString());
        bw.flush();
    }

    public static int check(int target) {
        int lower = lowerbound(arr1, target);
        int upper = upperbound(arr1, target, lower); 

        return upper - lower;
    }

    public static int lowerbound(int[] arr, int target) {
        int lower = 0;
        int upper = arr1.length;
        while (lower < upper) {
            int mid = (upper + lower) / 2;
            if (target <= arr1[mid]) {
                upper = mid;
            } else {
                lower = mid + 1;
            }
        }
        return lower;
    }

    public static int upperbound(int[] arr, int target, int lowerIdx) {
        int lower = lowerIdx;
        int upper = arr1.length;
        while (lower < upper) {
            int mid = (upper + lower) / 2;
            if (target >= arr[mid]) {
                lower = mid + 1;
            } else {
                upper = mid;
            }
        }
        return lower;
    }
}

์ด๋ ‡๊ฒŒ check ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“œ๋‹ˆ๊นŒ 24% ์ •๋„ ๋“ค์–ด๊ฐ”์„ ๋•Œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ์• ์ดˆ์— ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋Š” ํ‹€๋ฆฐ ์ฝ”๋“œ๋ผ๋Š” ๋ง์ด๋‹ค.๐Ÿค—
๊ทธ๋ž˜์„œ ์–ด๋””๊ฐ€ ํ‹€๋ ธ์„๊นŒ ํ™•์ธํ•˜๋Š” ์ค‘์— ๋„๋ฌด์ง€ ์•Œ ์ˆ˜ ์—†์–ด์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ์ฐธ๊ณ  ํ–ˆ๋Š”๋ฐ, lower / upper ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฑฐ์˜ ์ด์ง„ ํƒ์ƒ‰๊ณผ ๋น„์Šทํ•œ ์ฝ”๋“œ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

 

[BOJ] 10816. ์ˆซ์ž ์นด๋“œ 2 - (Java)

[BOJ] 10816. ์ˆซ์ž ์นด๋“œ 2 - (Java) Problem ์ œ์ถœ์ผ : 2020-04-02 ๋ฌธ์ œ ํ’€์ด ์‹œ๊ฐ„ : 30M ๋‚œ์ด๋„ : โ˜…โ˜…โ˜… link : https://www.acmicpc.net/problem/10816 ์ˆซ์ž ์นด๋“œ๋Š” ์ •์ˆ˜ ํ•˜๋‚˜๊ฐ€ ์ ํ˜€์ ธ ์žˆ๋Š” ์นด๋“œ์ด๋‹ค. ์ƒ๊ทผ์ด๋Š”..

herong.tistory.com

์ด ๋ถ„์˜ ๊ฒฝ์šฐ

  1. mid๊ฐ’์„ ๋น„ํŠธ ์—ฐ์‚ฐ(>>)์„ ํ†ตํ•ด ์ถ”์ถœํ•œ๋‹ค.
  2. target๊ฐ’์ด arr[mid]์™€ ๊ฐ™์„ ๋•Œ๋Š” lower๊ฐ’์„ mid + 1๋กœ ์„ค์ •ํ•œ๋‹ค.
  3. target๊ฐ’์ด arr[mid]๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ upper๊ฐ’์„ mid๋กœ ์„ค์ •ํ•œ๋‹ค.
  4. target๊ฐ’์ด arr[mid]๋ณด๋‹ค ํด ๊ฒฝ์šฐ lower๊ฐ’์„ mid + 1๋กœ ์„ค์ •ํ•œ๋‹ค.

๋‚˜์™€ ๊ต‰์žฅํžˆ ๋น„์Šทํ•˜๋‹ค. ๋‹ค๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์ด ๋‹ค๋ฅธ๋ฐ, ์œ„์˜ ๋งํฌ์—์„œ๋Š” lower ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ +1 ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉฐ check ๋ฉ”์†Œ๋“œ์—์„œ๋„ +1 ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•œ๋‹ค.
๋‚˜๋Š” ์•„์ง ์˜๋„๋ฅผ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์›๋ž˜ ๊ตฌํ˜„ ํ–ˆ๋˜ ๋ฐฉ์‹๋Œ€๋กœ +1 ์—ฐ์‚ฐ์„ ์ œ์™ธํ•˜๊ณ  ํ–ˆ๋”๋‹ˆ ๋” ๋น ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์คฌ๋‹ค.(์›ƒ๊ธฐ๋Š” ์งฌ๋ฝ•)

์•„๋งˆ๋„ 1๋ฒˆ์˜ ๊ฒฝ์šฐ ๋•Œ๋ฌธ์— +1 ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•ด์ค€ ๊ฒƒ ๊ฐ™๋‹ค. ์–ด์จŒ๋“  ์œ„ ๋ธ”๋กœ๊ทธ์˜ ์ฝ”๋“œ๋Š” ์ด์ง„ ํƒ์ƒ‰์˜ ์‘์šฉ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋งŒ ๋‚˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ ํ–ˆ๊ณ , ๋ธ”๋กœ๊ฑฐ ๋ถ„๊ป˜์„œ๋Š” while๋ฌธ์œผ๋กœ ๊ตฌํ˜„ ํ–ˆ๋‹ค์˜ ์ฐจ์ด์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ์žฌ๊ท€๋กœ ์ธํ•œ stackoverflow๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์ด์ „์˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ, ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด ์•„๋‹๊นŒ ์‹ถ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer sb = new StringBuffer();
    static int n, m;
    static int[] arr1;

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        int cnt = 0;
        arr1 = new int[n];
        while (st.hasMoreTokens()) {
            arr1[cnt] = Integer.parseInt(st.nextToken());
            cnt++;
        }

        Arrays.sort(arr1);

        m = Integer.parseInt(br.readLine());

        st = new StringTokenizer(br.readLine());
        cnt = 0;
        while (st.hasMoreTokens()) {
            int target = Integer.parseInt(st.nextToken());
            int result = check(target);
            sb.append(result + " ");
        }
        bw.write(sb.toString());
        bw.flush();
        bw.close();
    }

    public static int check(int target) {
        int lower = lowerbound(target);
        int upper = upperbound(target);

        return upper - lower;
    }

    public static int lowerbound(int target) {
        int lower = 0;
        int upper = n;
        while (lower < upper) {
            int mid = (upper + lower) / 2;
            if (target == arr1[mid]) {
                upper = mid;
            } else if (arr1[mid] > target) {
                upper = mid;
            } else {
                lower = mid + 1;
            }
        }
        return lower;
    }

    public static int upperbound(int target) {
        int lower = 0;
        int upper = n;
        while (lower < upper) {
            int mid = (upper + lower) / 2;
            if (target == arr1[mid]) {
                lower = mid + 1;
            } else if (target < arr1[mid]) {
                upper = mid;
            } else {
                lower = mid + 1;
            }
        }
        return upper;
    }
}

์ฆ‰, ์ง€๊ธˆ๊นŒ์ง€ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๋ฅผ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด

  1. ์‹œ๊ฐ„ ์ดˆ๊ณผ
    • ๋ง ๊ทธ๋Œ€๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ์ฒ˜์Œ ์ œ์ถœํ•œ ์ฝ”๋“œ๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ ํ–ˆ๋Š”๋ฐ, ์ด ๋•Œ target๊ณผ arr[mid]๊ฐ’์ด ๊ฐ™์„ ๊ฒฝ์šฐ arr[mid]์˜ ์•ž, ๋’ค ์ธ๋ฑ์Šค๋„ ๋‹ค์‹œ ํƒ์ƒ‰ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.
    • ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๋ถˆํ•„์š”ํ•œ ์žฌ๊ท€ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋˜๋ฉด์„œ ์ •ํ•ด์ง„ ์‹œ๊ฐ„๋ณด๋‹ค ์ดˆ๊ณผํ•ด์„œ ์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
  2. ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ
    • ์œ„์˜ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
    • ์ด ๊ฒฝ์šฐ๋„ ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ ํ–ˆ๋Š”๋ฐ arr[mid]์˜ ์•ž, ๋’ค ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ํƒ์ƒ‰ํ•  ๋•Œ ์ƒˆ๋กœ์šด ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋” ๋งŽ์ด ์†Œ์š”๋˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
  3. ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ
    • ์•„์ง๋„ ์ •ํ™•ํ•œ ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋‹ค.
    • ์˜ˆ์ƒ์œผ๋กœ๋Š” ์ธ๋ฑ์Šค ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋„˜๊ฑฐ๋‚˜ ์ ์€ ๊ฐ’์„ ํƒ์ƒ‰ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
  4. ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.
    • lower / upper ๋ฉ”์†Œ๋“œ์—์„œ target๊ณผ arr[mid]๋ฅผ ๋น„๊ตํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์„ ํ˜น์€ else ๋ฅผ ์ด์šฉํ•ด ๋‘ ๊ฐ€์ง€๋กœ๋งŒ ๊ตฌํ˜„ ํ–ˆ๋Š”๋ฐ ์ด ๊ฒฝ์šฐ์— ์ •ํ™•ํ•œ ๋ฐฉํ–ฅ์„ ์žก์ง€ ๋ชปํ•ด ์˜ณ์ง€ ๋ชปํ•œ ๋‹ต์„ ์ค‘๊ฐ„(24%)์— ์ถœ๋ ฅํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

๋„ˆ๋ฌด ๋‹ต๋‹ตํ–ˆ์ง€๋งŒ ๊ทธ๋ž˜๋„ ์ดํ•ดํ•˜๊ณ  ๋‚˜๋‹ˆ ์ฆ๊ฑฐ์› ๋‹ค. ๋˜ ๋ณด์ž! (๊ณ„์ˆ˜ ์ •๋ ฌ์„ ํ†ตํ•ด ํ•œ๋ฒˆ ํ’€์–ด ๋ด์•ผ๊ฒ ๋‹ค.)